Simply put, I had this working:
java -cp ".:bin:MyJar.jar" path.to.My.Main
but this threw an exception when trying to load a dynamically compiled class:
java -jar MyJar.jar
The failure was on
x = clazz.newInstance();
Where I get a “java.lang.NoClassDefFoundError:”. I loaded the class using a custom class loader, since the class was runtime compiled. The reason the exception was occurring was because java was using a separate classloader when trying to load imports inside the clazz. This is rectified by modifying your ClassLoader extends URLClassLoader to call super.loadClass(className, false);
Solution in Question:
Modify your custom classloader to call super.loadClass(). Even though the stack trace may not obviously pass through your classloader.