Forums > Java

adding a path to the classpath

October 8, 2007 | 11:34 am

hi all,

i’m trying to add a temporary path to the classpath in the init procedure of my application, but i can’t manage to do it.
the javadoc shows a "addDirectory" method for the MXJClassLoader class, but it’s protected. All i can do is getting the current paths…

Is there a way to do this?

G.


October 8, 2007 | 5:28 pm

i don’t think it was ever intended to be used that way but…
you could use reflection to call the method even though it
protected. good luck.
t

No look at reflection can be complete without talking about how to
break one of the most sacred of all the rules in Java. Everyone knows
that you cannot call a private method from outside of the class that
defines it. Right? Well you can’t if you stick to conventional
techniques, but with reflection you can do darn near anything.

The first thing you need to call a private method is the Method
instance that goes with the method you want to call. You can’t get
this from getMethod; it only returns public methods. The way to get
hold of a private (or protected) method is to use getDeclaredMethod.
While getMethod takes a client’s view of a class and only returns
public methods, getDeclaredMethod returns all of the methods declared
by one class. In the example below, we use it to get at the Method
object for the very private removeRange method on the
java.util.ArrayList class:

ArrayList list = new ArrayList();
list.add("Larry");
list.add("Moe");
list.add("Curley");

System.out.println("The list is: " + list);

Class klass = list.getClass();

Class[] paramTypes = { Integer.TYPE, Integer.TYPE };
Method m = klass.getDeclaredMethod("removeRange", paramTypes);

Object[] arguments = { new Integer(0), new Integer(2) };
m.setAccessible(true);
m.invoke(list, arguments);
System.out.println("The new list is: " + list);
Once you have a private method, calling it is a simple matter of
clicking off the final setAccessable safety and having at it:

On Oct 8, 2007, at 04:34 AM, guillaume wrote:

>
> hi all,
>
> i’m trying to add a temporary path to the classpath in the init
> procedure of my application, but i can’t manage to do it.
> the javadoc shows a "addDirectory" method for the MXJClassLoader
> class, but it’s protected. All i can do is getting the current
> paths…
>
> Is there a way to do this?
>
> G.
>


October 8, 2007 | 5:34 pm

hi topher, thanks for your help. i’m trying this kind of code right now :

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

/**
* Useful class for dynamically changing the classpath, adding classes during runtime.
* @author unknown
*/
public class ClasspathHacker {
/**
* Parameters of the method to add an URL to the System classes.
*/
private static final Class[] parameters = new Class[]{URL.class};

/**
* Adds a file to the classpath.
* @param s a String pointing to the file
* @throws IOException
*/
public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}//end method

/**
* Adds a file to the classpath
* @param f the file to be added
* @throws IOException
*/
public static void addFile(File f) throws IOException {
addURL(f.toURL());
}//end method

/**
* Adds the content pointed by the URL to the classpath.
* @param u the URL pointing to the content to be added
* @throws IOException
*/
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL",parameters);
method.setAccessible(true);
method.invoke(sysloader,new Object[]{ u });
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}//end try catch
}//end method
}

i think it should work soon…


Viewing 3 posts - 1 through 3 (of 3 total)