How to Change the Classpath Within Java

How do you change the CLASSPATH within Java?

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");

trying to change classpath through code?

According to this answer, there's no way to change the system classpath reliably. This other question suggests a way of loading JDBC drivers via classloaders
(Direct link: http://www.kfu.com/~nsayer/Java/dyn-jdbc.html).

How to change the classpath and run the code in Java?

Assuming that /Anto is really your home directory, try this:

javac -classpath ~/RSyntaxTextArea.jar TextEditorDemo.java

Otherwise, just point to a relative path to the jar file. For one thing, you were trying to use \, where in Linux you should be using /. You can reference the current directory with . So, if the jar is in your current working directory, you can just do this:

javac -classpath RSyntaxTextArea.jar TextEditorDemo.java

Or this:

javac -classpath ./RSyntaxTextArea.jar TextEditorDemo.java

If the Anto directory is under the current directory, use this:

javac -classpath ./Anto/RSyntaxTextArea.jar TextEditorDemo.java

How to change the classpath in command prompt?

As per my understanding you want to change the classpath which you have set by command

set classpath=d:java

can be done
in two ways either you can set classpth directly as environment varible by

--> Right click on my computer select advanced options

--> there you will see option as environment variables open that option

--> now you will see multiple variables being set...search for classpath variable if it exist
their edit this variable value by just putting semicolon and write ur full classpath ended by semicolon and save it.

FOR EG:-

variable name:- CLASSAPTH
variable value- .;C:\Oracle\product\10.1.0\Client_2\jdbc\lib\ojdbc14.jar;

Second option is just set your class in command promt as u have set earlier

by opening command prompt

set classpath=C:\Oracle\product\10.1.0\Client_2\jdbc\lib\ojdbc14.jar;

both the method are easy but i would prefer you should go with first one as by doing that you didn't need to set your classpath again and again after you reboot your system or application.

How to set multiple classpath entries in java at runtime?

In addition to @Szmeby answer, if you don't know how to use file with classpath inside, you may try to create a "pathing jar".

"Pathing jar" contains only Manifest.mf file which includes next entry:

Class-Path: some.jar another.jar others.jar

You can also use wildcards to reduce length.



Related Topics



Leave a reply



Submit