Dynamically load class at runtime [message #659133] |
Fri, 11 March 2011 03:12  |
Eclipse User |
|
|
|
Hello,
I am working on an eclipse plugin and I need to load a directory into the classpath at runtime. It's only possible at runtime!
I tried to do the following:
public static void addDirectoryToClassPath(String pDirectory) throws Exception {
File lDirectoryToAdd = new File(pDirectory);
if(!lDirectoryToAdd.exists() || !lDirectoryToAdd.isDirectory()) {
throw new Exception(pDirectory + " doesn't exist or isn't a directory!");
} else {
Thread lCurrentThread = Thread.currentThread();
ClassLoader lOldClassLoader = lCurrentThread.getContextClassLoader();
URL[] lDirectoryURL = new URL[] {lDirectoryToAdd.toURI().toURL()};
URLClassLoader lNewClassLoader = new URLClassLoader(lDirectoryURL, lOldClassLoader);
lCurrentThread.setContextClassLoader(lNewClassLoader);
}
}
But if I try to load the class later via
Class.forName("classname);
I get a CLassNotFoundException.
For Information: I will load the class in another eclipse plugin, but that's not the problem at the moment because even in the same plugin Class.forName("") doesn't work...
I would appreciate any help
Thanks in advance
Alex
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.26196 seconds