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]

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
> 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);
> 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?