2

Possible Duplicate:
Setting CLASSPATH during runtime

POSSIBLE DUPLICATE: Setting CLASSPATH during runtime

To get the classpath I am using :

 ClassLoader cl = ClassLoader.getSystemClassLoader();
     URL[] urls = ((URLClassLoader)cl).getURLs();
     for(URL url: urls){
         System.out.println("classpath:"+url.getFile());
         }

Can some one help me with the code to load the jars in classpath at runtime.

ClassLoader currentThreadClassLoader
     = Thread.currentThread().getContextClassLoader();
    URLClassLoader urlClassLoader
     = new URLClassLoader(new URL[]{new File("D:\\ms.jar").toURL()},
                          currentThreadClassLoader);
    Thread.currentThread().setContextClassLoader(urlClassLoader);

This doesent seem to work.

Community
  • 1
  • 1
user1731553
  • 1,887
  • 5
  • 22
  • 33
  • Set the CLASSPATH appropriately before the app starts and make the JARs part of it. I don't see a reason why this can't be sufficient. Runtime loading of JARs is for app servers, not you and me. – duffymo Oct 18 '12 at 20:43
  • 1
    If I develop an application which load plugins one the fly, the suggested mechanism will be used and the classpath may be important – Aubin Oct 18 '12 at 20:47
  • 1
    http://stackoverflow.com/questions/2899804/setting-classpath-during-runtime possible duplicate? – Aniket Inge Oct 18 '12 at 20:48
  • 1
    _"This doesent seem to work."_. Exception? – betomontejo Oct 18 '12 at 20:49
  • No exception but when I try to print the current classpath I cant see the added jar – user1731553 Oct 18 '12 at 20:51
  • @PrototypeStark the suggested duplicate question is not exactly a duplicate. But the answer to the question you pointed out is an answer to this question if I understand it well. – Alban Oct 18 '12 at 20:51
  • 1
    @Alban although it isn't a 1:1 copy, its definitely the answer he should have found while he "googled". – Aniket Inge Oct 18 '12 at 20:53
  • `File("...").toURL()` has been deprecated since Java 1.2! Are you compiling using a 1.1 SDK or simply ignoring warnings? – Andrew Thompson Oct 18 '12 at 20:55
  • as of now ignoring.... I need this working :( – user1731553 Oct 18 '12 at 21:06

2 Answers2

1

A complete (coded) solution would be a bit beyond a single Stack Overflow answer, so I'll outline the points you need to be aware of instead if you decide to write your own ClassLoader:

  1. The classloader represents (part of) a namespace, and two otherwise identical classes loaded by different classloaders are not "equal". Which means there are a few dangers lurking in classloading, notably singletons suddenly not being so single anymore as well as casts failing unexpectedly.
  2. Classloaders (should) work on a pattern of delegating to the "parent" loader before attempting anything themselves (see above).
  3. Class loading and linking are two distinct steps (even though example implementations of a classloader such as may be found in blog posts/online Java articles, will combine the two into one for simplicity) Therefore you should not assume that if a parent loader has loaded a class it has also loaded all dependencies ...
  4. All this means there is a problem if class A loaded by loader A references a class B which neither loader A nor any of its parents can load: class A may load just fine in loader A but at the point of use it fails because loader A cannot fully resolve (link) it.
  5. And you should make sure that your classloader loads classes in a synchronized manner otherwise the issues hinted at in step #1 can leap from duplicates due to classloaders to duplicates from multiple threads using the same classloader as well...

Note: it is far easier to just use the -cp switch in some wrapper script/batch file for your program.

user268396
  • 11,576
  • 2
  • 31
  • 26
0

From the ClassLoader#getSystemClassLoader() doc:

This method is first invoked early in the runtime's startup sequence, at which point it creates the system class loader and sets it as the context class loader of the invoking Thread.

When you do Thread.currentThread().setContextClassLoader(urlClassLoader), you're changing the reference in the current thread, not the one in ClassLoader (and you can't change this one), so from then on you should rely on the new class loader of the current thread to load your classes with something like:

Thread.currentThread().getContextClassLoader().loadClass(...) 
betomontejo
  • 1,657
  • 14
  • 26