entanglement 4 de jun. de 2010
Acho que você vai ter de achar o erro neste programa aqui.
import java.util.* ;
import java.io.* ;
import java.lang.reflect.* ;
public class ByteClassLoader extends ClassLoader {
@Override
public Class <?> findClass ( String name ) {
Class <?> klass = nameToClass . get ( name );
if ( klass != null ) return klass ;
byte [] bytes = nameToBytes . get ( name );
assert ( bytes != null );
klass = defineClass ( name , bytes , 0 , bytes . length );
nameToClass . put ( name , klass );
return klass ;
}
public void addBytes ( String name , byte [] bytes ) {
nameToBytes . put ( name , bytes );
}
private Map < String , byte []> nameToBytes = new HashMap < String , byte []> ();
private Map < String , Class > nameToClass = new HashMap < String , Class > ();
}
class TesteByteClassLoader {
public static void main ( String [] args ) throws Exception {
ByteClassLoader bcl = new ByteClassLoader ();
File f = new File ( "Teste.class" );
InputStream is = new FileInputStream ( f );
byte [] bytes = new byte [ ( int ) f . length () ] ;
is . read ( bytes );
is . close ();
bcl . addBytes ( "Teste" , bytes );
Class <?> klass = bcl . findClass ( "Teste" );
Method mainMethod = klass . getDeclaredMethod ( "main" , String [] . class );
mainMethod . invoke ( null );
}
}
class Teste {
public static void main ( String [] args ) {
System . out . println ( "Hello, world!" );
}
}
O que você deve fazer é ver por que ocorre este erro aqui:
Exception in thread "main" java . lang . IllegalAccessException : Class TesteByteClassLoader can not access a member of class Teste with modifiers "public static"
at sun . reflect . Reflection . ensureMemberAccess ( Unknown Source )
at java . lang . reflect . Method . invoke ( Unknown Source )
at TesteByteClassLoader . main ( ByteClassLoader . java : 34 )
Estou sem tempo de ver por que é que está ocorrendo isso, mas a ideia geral é dada acima.
zerokelvin 4 de jun. de 2010
PUBLIC class Teste{
resolve esse problema , mas dae vem outro :
Exception in thread “ main ” java . lang . IllegalArgumentException : wrong number of arguments
at sun . reflect . NativeMethodAccessorImpl . invoke0 ( Native Method )
at sun . reflect . NativeMethodAccessorImpl . invoke ( NativeMethodAccessorImpl . java : 39 )
at sun . reflect . DelegatingMethodAccessorImpl . invoke ( DelegatingMethodAccessorImpl . java : 25 )
at java . lang . reflect . Method . invoke ( Method . java : 597 )
at TesteByteClassLoader . main ( TesteByteClassLoader . java : 16 )
entanglement 4 de jun. de 2010
Talvez seja isto aqui:
class TesteByteClassLoader {
public static void main ( String [] args ) throws Exception {
ByteClassLoader bcl = new ByteClassLoader ();
File f = new File ( "Teste.class" );
InputStream is = new FileInputStream ( f );
byte [] bytes = new byte [ ( int ) f . length () ] ;
is . read ( bytes );
is . close ();
bcl . addBytes ( "Teste" , bytes );
Class <?> klass = bcl . findClass ( "Teste" );
Method mainMethod = klass . getMethod ( "main" , String [] . class );
mainMethod . setAccessible ( true );
mainMethod . invoke ( null , ( Object ) new String [] {});
}
}
O resultado esperado:
zerokelvin 4 de jun. de 2010
É iso ae! valeu memo, tá tudo funfando!