How do you change the CLASSPATH within Java?
Asked Answered
V

6

66

How do you change the CLASSPATH of a Java process from within the Java process?


Before you ask me "Why would you want to do that?" I'll explain it shortly.

When you have a Clojure REPL running it is common to need more jars in your CLASSPATH to load a Clojure source file, and I'd like to do it without having to restart Clojure itself (which is not really an option when using it on Slime on Emacs).

That's the reason but I don't want this question tagged as some-weird-language some-weird-editor and be disregarded by the majority of Java developers that may have the answer.

Vikkivikky answered 31/10, 2008 at 8:44 Comment(0)
P
85

Update 2023: as commented below by Holger

The only way to add jar files to the class path working in Java 9 and newer, is through the Instrumentation API, but it requires a Java Agent.

If you are in control of the launcher/main jar, you can use the Launcher-Agent-Class attribute in the jar file’s manifest to start an embedded Agent.


Update Q4 2017: as commented below by vda8888, in Java 9, the System java.lang.ClassLoader is no longer a java.net.URLClassLoader.

See "Java 9 Migration Guide: The Seven Most Common Challenges"

The class loading strategy that I just described is implemented in a new type and in Java 9 the application class loader is of that type.
That means it is not a URLClassLoader anymore, so the occasional (URLClassLoader) getClass().getClassLoader() or (URLClassLoader) ClassLoader.getSystemClassLoader() sequences will no longer execute.

java.lang.ModuleLayer would be an alternative approach used in order to influence the modulepath (instead of the classpath). See for instance "Java 9 modules - JPMS basics".


For Java 8 or below:

Some general comments:

you cannot (in a portable way that's guaranteed to work, see below) change the system classpath. Instead, you need to define a new ClassLoader.

ClassLoaders work in a hierarchical manner... so any class that makes a static reference to class X needs to be loaded in the same ClassLoader as X, or in a child ClassLoader. You can NOT use any custom ClassLoader to make code loaded by the system ClassLoader link properly, if it wouldn't have done so before. So you need to arrange for your main application code to be run in the custom ClassLoader in addition to the extra code that you locate.
(That being said, cracked-all mentions in the comments this example of extending the URLClassLoader)

And you might consider not writing your own ClassLoader, but just use URLClassLoader instead. Create a URLClassLoader with a url that are not in the parent classloaders url's.

URL[] url={new URL("file://foo")};
URLClassLoader loader = new URLClassLoader(url);

A more complete solution would be:

ClassLoader currentThreadClassLoader
 = Thread.currentThread().getContextClassLoader();

// Add the conf dir to the classpath
// Chain the current thread classloader
URLClassLoader urlClassLoader
 = new URLClassLoader(new URL[]{new File("mtFile").toURL()},
                      currentThreadClassLoader);

// Replace the thread classloader - assumes
// you have permissions to do so
Thread.currentThread().setContextClassLoader(urlClassLoader);

If you assume the JVMs system classloader is a URLClassLoader (which may not be true for all JVMs), you can use reflection as well to actually modify the system classpath... (but that's a hack;)):

public void addURL(URL url) throws Exception {
  URLClassLoader classLoader
         = (URLClassLoader) ClassLoader.getSystemClassLoader();
  Class clazz= URLClassLoader.class;

  // Use reflection
  Method method= clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
  method.setAccessible(true);
  method.invoke(classLoader, new Object[] { url });
}

addURL(new File("conf").toURL());

// This should work now!
Thread.currentThread().getContextClassLoader().getResourceAsStream("context.xml");
Podophyllin answered 31/10, 2008 at 9:37 Comment(11)
The "more complete" solution does not work for me using Java 1.6.0_13 (64-bit) on Linux. The addURL method, on the other hand, works as expected.Laidlaw
This works to add jar. But is there a solution to remove a jar? For example, I need to update a jar with a new release.Shackle
@Shackle 6+ years later, I don't know. Try asking a separate question.Podophyllin
I meet this solution in other thread but with your explaintion, I know clearly to use it. 1+Demibastion
The second solution (assume JVMs system classloader is a URLClassLoader) stops working in JRE 9. System class loader is no longer a URL class loader.Facesaving
@Facesaving Thank you. I have included your comment in the answer for more visibility. As well as some references.Podophyllin
The only way to add jar files to the class path working in Java 9 and newer, is through the Instrumentation API but it requires a Java Agent. If you are in control of the launcher/main jar, you can use the Launcher-Agent-Class attribute in the jar file’s manifest to start an embedded Agent.Postmark
@Postmark Thank you so much for that feedback. I have included your comment in the answer for more visibility.Podophyllin
@Postmark I came across your suggestion of using Instrumentation API. But that API only seems to have methods to append jars to the search path. I want my app's main.jar Main class to load a newer version of an app.jar if it exists in the user's directory. Since both main.jar and the original app.jar are in the system class loader's class path, the updated app.jar would need to be prepended to the class path, not appended, for the newer classes to be loaded. Is this possible with the Instrumentation API - or any API for that matter?Crazyweed
@Crazyweed Maybe a custom ClassLoader that you create would work: it would first search the updated JAR before delegating to the parent class loader. Or a custom child ClassLoader that has no parent (or a different parent without the conflicting JAR) to load the updated JAR. Other approach: rename the package of the updated classes, so they don't clash with the existing ones. You could then use some runtime logic to determine which version to use (class shadowing).Podophyllin
@Crazyweed there’s no support for that. On the other hand, you can simply start the application with the right class path in the first place, always having “user's directory”/app.jar at the beginning. If the file doesn’t exist, it has no effect.Postmark
T
3

I don't believe you can - the right thing to do (I believe) is create a new classloader with the new path. Alternatively, you could write your own classloader which allows you to change the classpath (for that loader) dynamically.

Telles answered 31/10, 2008 at 8:49 Comment(0)
G
2

You may want to look into using java.net.URLClassLoader. It allows you to programmatically load classes that weren't originally in your classpath, though I'm not sure if that's exactly what you need.

Gladiator answered 31/10, 2008 at 9:36 Comment(0)
S
2

There's no need to write your own class loader! There's clojure.lang.DynamicClassLoader.

http://blog.japila.pl/2011/01/dynamically-redefining-classpath-in-clojure-repl/

Smallminded answered 20/7, 2011 at 11:57 Comment(0)
E
1

It is possible as seen from the two links below, the method VonC gives seems to be the best but check out some of these posts and google for "Java Dynamic Classpath" or "Java Dynamic Class Loading" and find out some info from there.

I'd post in more depth but VonC has pretty much done the job.

From Dynamic loading of class and Jar files.

Also check this sun forum post.

Extremely answered 31/10, 2008 at 10:23 Comment(0)
S
-4
String s="java  -classpath abcd/ "+pgmname+" "+filename;   
Process pro2 = Runtime.getRuntime().exec(s); 
BufferedReader in = new BufferedReader(new InputStreamReader(pro2.getInputStream()));

is an example of changin the classpath in java program

Sundin answered 10/10, 2013 at 10:17 Comment(2)
The classpath is only changed for the child java process, not the environment that your code runs in. This is somewhat creative but unuseful for most.Karolekarolina
It is an example of starting a new Java process with a new classpath. Not an example of changing anything.Detect

© 2022 - 2024 — McMap. All rights reserved.