0

Setting a java class path not by using command prompt by using a java program. Everywhere we have description about how to set path and class path using cmd or by going to environment variables but I want to set the class path using a java program

1 Answers1

0

You can set the classpath when invoking a Java process. AFAIK you cannot modify the classpath within a running java program. So there are two ways to deal with that:

Invoke another JVM

Construct the command as you would have passed it on the command line, then use ProcessBuilder to run this command. It will create another process that runs your application. Noone needs to know your application is still in memory with 'the wrong' classpath.

Use a different Classloader

Create a Classloader that loads classes from your desired classpath. Load the classes in question through that classloader and invoke them as needed. See also Java class loader tutorial With this solution the operating system sees only one process.

This technique is commonly used in application servers or systems with a plugin architecture. You could therefore check out e.g. Apache Felix.

Queeg
  • 7,748
  • 1
  • 16
  • 42