0

I have code(it's wrapper for another applicaton):

URLClassLoader classLoader = new URLClassLoader(urls);
Class<?> mainClass = classLoader.loadClass("org.ololo.DummyClass");
Method main = mainClass.getMethod("main", new Class[]{
                    String[].class
});
main.invoke(null, new Object[]{args_for_client});

urls--contains all jars for application. some jars contains spring contexts for singleton initialization.. it's 3d party code. if I execute my wrapper code from cdm like: java -jar MyJar.jar application failed with classnotfoundexception (Spring cant find class from jar that was in the classLoader if I execute my wrapper code from cdm like: java -cp %CLASSES%-jar MyJar.jar
where %CLASSES% contains same jar as urls for classloader application work without any problems

kain64b
  • 2,258
  • 2
  • 15
  • 27
  • And why should spring see those classes? They are only visible in the classloader you constructed and that classloader isn't visible for spring. Is there a reason why you don't simply add the classpath and main class to the manifest file? That way you have an executable jar without your own hacks... – M. Deinum May 21 '14 at 07:49
  • I cant add classpath, because, I dont know where my jars. I have automatic update procedure: 1)download zip with jars, 2)unpack it into new folder 3)create classloader and execute application. but any way my question was: which difference between "-cp option" and classloader? – kain64b May 21 '14 at 08:06
  • Does it work if you install your custom class loader as the thread context loader (`Thread.currentThread().setContextClassLoader(classLoader)`) before calling the `main` method? – Ian Roberts May 21 '14 at 08:25
  • Using -cp adds it to the general classpath, when you use your own classloader it is only visible for that classloader. The latter isn't used for spring as you don't pass it along or set it as some parent classloader etc. Working with classloaders yourself is tricky and error prone. – M. Deinum May 21 '14 at 08:48
  • http://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html#URLClassLoader(java.net.URL[]) public URLClassLoader(URL[] urls) Constructs a new URLClassLoader for the specified URLs **using the default delegation parent** ClassLoader. M. Deinum rtm ). – kain64b May 21 '14 at 10:50

0 Answers0