problems with URLClassLoader

while studying RMI dynamic classloading i was trying to see at first if i knew how to dynamically execute a classfile,once i had it already in my classpath.

the class i am trying to load and execute is called Inc.class:

the constructor is: Inc(int secs) //

and has one method called:int execute(int i) //does a Thread.sleep(secs*1000) and takes the int and adds 1 to it and returns the result

Now,i'm trying to dynamically load this class in an other class with the following steps:

//imported packages

//class declaration

{

File f = new File(<classpath>);

try {

URL[] url = {f.toURL()};

ClassLoader cl = new URLClassLoader(url);

Class c = cl.loadClass("Inc");

System.out.println("Class loaded");

Object o = c.newInstance();

int [] dati = new int[1];

dati [0] = 12;

Inc co = (Inc) o;

System.out.println("co(12) = "+co.exec(dati));

}//followed by all the occuring exceptions

it compiles,but then when i excecute it, it gives me the following output with the runtime exceptions:

Class loaded

java.lang.InstantiationException: Inc

at java.lang.Class.newInstance0(Class.java:335)

at java.lang.Class.newInstance(Class.java:303)

at ClassLoading.main(ClassLoading.java:20)

i thought it would be simpler,but i even in this basic classloading example i don't know where to focus on to let it run clearly.

other problem is>how can i pass the Inc constructor its parameter if i have to cast it from the c.newInstace line,which returns an Object?

hope i made the point,thanxalot,nicholas

[1663 byte] By [Nihcolasa] at [2007-11-26 19:58:17]
# 1
don't use Class.newInstance, since that needs an empty constructor. find the right Constructor for that class and use Constructor.newInstance
georgemca at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 2

Also; a URLClassLoader will delegate the loading of any classes which are on the class path - it will only load classes from the url when it's had a ClassNotFound exeception back from the system ClassLoader.

To ensure that a class is loaded by a URLClassLoader you need to remove it from the class path.

malcolmmca at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 3

problem is,how can i load a constructor form a classfile?

while java.lang.Class has this particular method

-->Class c = cl.loadClass("Inc"); //as above

java.lang.reflect.Constructor doesn't,and i can't figure out how to load it up.

could you help me with that?

thanxalot,nicholas

Nihcolasa at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 4
> problem is,how can i load a constructor form a> classfile?You don't load a constructor. You load the class, and its constructors are part of that.
jverda at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 5

i tried to get a Constructor from a loaded class,but still gives me problems,i'm still a bit confused on how dynamically load a class file and get an instance of its Object and passing the constructor its parameters if needed

//same as above plus: import java.lang.reflect.*;

try{

URL[] url = {f.toURL()};

ClassLoader cl = new URLClassLoader(url);

Class c = cl.loadClass("Inc");

System.out.println("Class Loaded");

>Constructor o =(Constructor) c.newInstance(); <

int [] dati = new int[1];

dati [0] = 12;

>Inc co = (Inc) o.newInstance(13); <-

System.out.println("co(12) = "+co.exec(dati));}

//catching all the exceptions

it compiles,but when i run the program it gives me these exceptions

Class Loaded

java.lang.InstantiationException: Inc

at java.lang.Class.newInstance0(Class.java:335)

at java.lang.Class.newInstance(Class.java:303)

at ClassLoading.main(ClassLoading.java:22)

thanx again,nicholas

Nihcolasa at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 6
> >Constructor o =(Constructor) c.newInstance();what do you expect that to do? Constructors are a bit like Methods, you look them up on a Class object in much the same way. read Teh Docs ™ for more info
georgemca at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 7

newInstance from the class attempts to create an instance of the class using a no-args constructor. What you need is the Class.getConstructor() method, which will find a constructor to match your argument list classes. e.g.

Constructor cc = c.getConstructor(new Class[]{Integer.TYPE});

Inc inc = (Inc)cc.newInstance(new Object[]{new Integer(13)});

malcolmmca at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 8

it finally works!!

last thing i've tried is to run the main class with the classloader from different classpath to see if the URLClassloader works by searching the class he has to load in the parameter path...

..RESULT>ClassNotFound Exception!!

could you help me with this too?

thanxalot!!

Nihcolasa at 2007-7-9 22:53:51 > top of Java-index,Java Essentials,Java Programming...
# 9

> newInstance from the class attempts to create an

> instance of the class using a no-args constructor.

> What you need is the Class.getConstructor() method,

> which will find a constructor to match your argument

> list classes. e.g.

>

> > Constructor cc = c.getConstructor(new

> Class[]{Integer.TYPE});

> Inc inc = (Inc)cc.newInstance(new Object[]{new

> Integer(13)});

>

note that if you are using Java 5 or above, that code could also be written thus:

Constructor<Inc> cc = c.getConstructor(Integer.TYPE);

Inc inc = cc.newInstance(13);

georgemca at 2007-7-9 22:53:52 > top of Java-index,Java Essentials,Java Programming...
# 10

> it finally works!!

>

> last thing i've tried is to run the main class with

> the classloader from different classpath to see if

> the URLClassloader works by searching the class he

> has to load in the parameter path...

>

> ..RESULT>ClassNotFound Exception!!

>

> could you help me with this too?

>

> thanxalot!!

are you sure you're specifying the correct URLs?

georgemca at 2007-7-9 22:53:52 > top of Java-index,Java Essentials,Java Programming...