I have a jar, which contains all needed external libs. All i want: load libraries in runtime so i can use them as i could if they were in classpath.
Some visualization:
-main.jar
-lib.jar
--apache.jar
--someother.jar
---aclass.class
etc.
Some not really helpful code:
// loading libraries
ClassLoader contextCL = Thread.currentThread().getContextClassLoader();
ArrayList<URL> librariesJarsURL = new ArrayList<>();
JarFile libJar = new JarFile(libBundle);
Enumeration libJarEnum = libJar.entries();
while (libJarEnum.hasMoreElements()) {
JarEntry libJarEntry = (JarEntry) libJarEnum.nextElement();
if (!libJarEntry.isDirectory() && libJarEntry.getName().endsWith(".jar")) {
librariesJarsURL.add(new URL("jar:file://" + (libBundle.getAbsolutePath().replace("\\", "/")) + "!/" + libJarEntry.getName()));
}
}
if (librariesJarsURL.size() > 0) {
for (URL libraryJarURL : librariesJarsURL) {
System.out.println(libraryJarURL);
}
ClassLoader urlCL = URLClassLoader.newInstance(librariesJarsURL.toArray(new URL[0]), contextCL);
Thread.currentThread().setContextClassLoader(urlCL);
}
System.out.println(StringUtils.difference("zero", "zero141"));
The last is from Commons Apache String utils that also should be included but aint giving me NoClassDefFoundError and ClassNotFoundException.
Also, i tried to use "reflection hack" but it aint working as well.
Thats the problem, guys... Any ideas?
EDIT: libBundle is kinda File object...
UPD:
/*
* Reflection hack :/
*/
public static void addURL(URL url) throws Exception
{
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class uclclass = URLClassLoader.class;
// Use reflection
Method method = uclclass.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(classLoader, new Object[]{url});
}
Used libBundle.toURI().toURL()
- same errors,
addURL(new URL("jar:file://" + (libBundle.getAbsolutePath().replace("\\", "/")) + "!/"));
- thats same,
tried to do same with all jars in lib.jar - same.
Tried URLClassLoader libraryLoader = new URLClassLoader(librariesJarsURL.toArray(new URL[0]), GlobalInit.class.getClassLoader());
- same.