Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java classloading running extremely slow?

Tags:

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..