I’m trying to load a java .class file dynamically and call it by reflection.
I’ve got a class called Foo; it has an empty constructor and has one method called doit() which takes a String argument and returns a String. Also it reverses the String.
Here is my code:
URL url = new URL("file://C:/jtest/");
URLClassLoader loader = new URLClassLoader(new URL[]{url});
Class<?> cl = loader.loadClass("Foo");
Constructor<?> cons = cl.getConstructor((Class[])null);
Object ins = cons.newInstance(new Object[]{});
Method meth = cl.getDeclaredMethod("doit", String.class);
Object ret = meth.invoke(ins, new Object[]{"!dlroW olleH"});
System.out.println((String)ret);
As expected this prints “Hello World!”. However, it takes about 30 seconds to complete. I know reflection is slow, but I expect it to be 10 ms or something.
I’m using Eclipse with JRE 1.6.0_13, and I’m running Windows Vista.
What am I doing wrong here?
Thanks.
Edit: I’ve profiled the code, and all of its time is used in the third line(loadClass()). Everything else happens instantly.
Edit: I’ve put the code in a loop; the slow function somehow gets optimized and takes 30 seconds only on the first loop.
Edit: I’ve found the solution.
Instead of:
URL url = new URL("file://C:/jtest/");
I changed it to:
URL url = new URL("file:/C:/jtest/");
Now it works perfectly. I don’t know why it works, but I don’t see how I (and 5 other people) could have missed that. Now I feel dumb..
At 30 seconds, you should be able to “profile” your code and see exactly where the problem lies (in loading of the class? in creating the instance? in looking up the method? etc?)
Since it’s taking 30 seconds (and not something much smaller, on the order of 10ms or so), you can just use
System.out.println(new Date());between each line of your code.I suspect you’ll find it’s the loader.loadClass(String) taking so long – and I suspect that you’ll find you either have a very long classpath, or a classpath that includes a network resource of some sort.