I'm invoking another jar file using reflection like this mainMethod.invoke(null, new Object[] { passargs });
(basic stuff). It works for most jar files, but for one I keep getting a ClassNotFoundException
. It's erroring out on the first line of code in the external jar which is creating a new object defined in that jar (although the other classes load their own jar-classes fine). Since the error is occuring on the first line of the jar's main method, I don't think it's using any custom classloaders.
Any ideas what could cause a ClassNotFoundException when calling the main method of another jar? This works for many other complex jars. Just one of them isn't working, and I'm wondering what could be the cause. It works fine when I executed it individually with java -jar
//my jar
JarFile jar = new JarFile(load);
Attributes attributes = jar.getManifest().getMainAttributes();
String mainClass = attributes.getValue("Main-Class");
URLClassLoader cl = new URLClassLoader(new URL[] { new File(load).toURI().toURL() }, ClassLoader.getSystemClassLoader());
Class main = cl.loadClass(mainClass);
System.out.println("MAIN CLASS: " + mainClass);
Method mainMethod = main.getDeclaredMethod("main", new Class[] { String[].class });
mainMethod.invoke(null, new Object[] { new String[0] });
It throws the exception on this line (Other jar - one being invoked)
public static void main(String[] args) throws Exception {
LoadingScreen ls = new LoadingScreen();
Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/spoutcraft/launcher/gui/LoadingScreen
at org.spoutcraft.launcher.Main.main(Main.java:106)
at com.mineshaftersquared.MineshafterSquaredGUI.wrapJar(Driver.java:110)
at com.mineshaftersquared.resources.ServerProxy.main(ServerProxy.java:71)
Caused by: java.lang.ClassNotFoundException: org.spoutcraft.launcher.gui.LoadingScreen
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 3 more