-
Setting class path at runtime
Hi,
Anyone knows how to set the class path at runtime? I mean after JVM has started. May be to a directory or a jar file.
I tried
Code:
System.setProperty("java.class.path");
. But didn't work.
Regards;
Stephen
-
I dont' think you can change the classpath once the JVM is started.
What are you trying to do? Is there another solution, say by using the URLClassLoader?
-
Y don't you try System.Runtime.
The below link might be useful
http://java.sun.com/docs/books/tutor...m/runtime.html
I am 75% sure that you can set a classpath. i meant you can append a classpath.I said append because to run Java class you should have already created a classpath and given the value.
Thanks
G.Rajasekhar
-
gulapala,
Could you give an example of how a running program that is currently executing in a JVM can change the classpath that the JVM is using?
For example if the JVM is started with classpath = .
how would you change it to: classpath = here/there;.
-
As norm said, you cannot change classpath at runtime. Its a read-only property.
Your parameter is indeed correct i.e.
Code:
System.getProperty("java.class.path");
will return classpath.
You are not getting any exception in trying to set this property cause you has n't specified and SecurityManager.
Regards,
Mohit
-
By using URL classloader concepts.
-
Please give me some time. I am very busy with my work in my office and so not able to find time to actually write the stuffs.
But yes you can do it. and from one classloader you can change the path.
-
In your demo of changing the classpath, show it before, change it and then show it after:
System.outprintln("before" +System.getProperty("java.class.path"));
// here change the classpath
System.outprintln("after" + System.getProperty("java.class.path"));
Using URLClassLoader doesn't change the classpath. It uses a brand new one!
-
oh... I think I was wrong.
You can change the classloader of a different one but not the one which we are working on. Anyway I have to try the latter one.
Thanks
G.Rajasekhar
-
hai everyone,
I think What I said correct. I am sure that the classpath can be changed and I am herewith giving the demo version.
Assumptions:
------------
1. I have many paths set to my class paths.
2. I have only one jvm running, where I am running this program.
The program :
import java.io.*;
class Example2
{
public static void main(String args[])
{
System.out.println("Before the path" + System.getProperty("java.class.path"));
System.setProperty("java.class.path", "E:\\raja");
System.out.println("\n\n\n");
System.out.println("AFterthe path" + System.getProperty("java.class.path"));
}
}
The output in my program:
-------------------------
E:\raja\JavaPrograms>javac Example2.java
E:\raja\JavaPrograms>java Example2
Before the path%classpath%;.;D:\JavaWebSfts\jakarta-tomcat-5.0.28\bin;.;d:\j2sdk
1.4.2_05\bin;.;D:\JavaWebSfts\jakarta-tomcat-5.0.28\common\lib\servlet-api.jar;.
;D:\JavaWebSfts\jakarta-struts-1.2.4-lib\struts.jar;.;D:\bea\weblogic81\server\l
ib\weblogic;.;;.;D:\JavaWebSfts\jakarta-tomcat-5.0.28\bin;.;d:\j2sdk1.4.2_05\bin
;.;D:\JavaWebSfts\jakarta-tomcat-5.0.28\common\lib\servlet-api.jar;.;D:\JavaWebS
fts\jakarta-struts-1.2.4-lib\struts.jar;.;D:\bea\weblogic81\server\lib\weblogic;
.;;.;D:\JavaWebSfts\jakarta-tomcat-5.0.28\bin;.;d:\j2sdk1.4.2_05\bin;.;D:\JavaWe
bSfts\jakarta-tomcat-5.0.28\common\lib\servlet-api.jar;.;D:\JavaWebSfts\jakarta-
struts-1.2.4-lib\struts.jar;.;D:\bea\weblogic81\server\lib\weblogic;.;;.;
AFterthe pathE:\raja
E:\raja\JavaPrograms>
When you are setting the classpath using the setproperty, you have to give the property and the value. When giving the value, \ should be escape character, so when setting the classpath to E:\raja doesn't give the correct classpath, But you have to give E:\\raja.
Thanks
G.Rajasekhar
-
That's part of a demo.
The real test is having the JVM use the new classpath!
Can you demo that?
-
Here's my demo. Two files are tested for using getResource() one on before classpath and one on afer classpath. The one on before CP is found after the change, the one on the after CP is NOT found.
Running: java -Xms64M SetClassPath
Before path:.;D:\JavaDevelopment\;D:\Java\jsdk2.0\lib\jsdk.jar;E:\JAVA\JMF21~1.1E\LIB\SOUND.JAR;E :\JAVA\JMF21~1.1E\LIB;E:\JAVA\JMF21~1.1E\LIB\JMF.JAR;C:\JAVA\BDK1.0\DEMO
After path:E:\Temp\
url=null
url=file:/D:/JavaDevelopment/comm.jar
0 error(s)
Code:
import java.io.*;
import java.net.*;
class SetClassPath {
public SetClassPath() {
System.out.println("Before path:" + System.getProperty("java.class.path"));
System.setProperty("java.class.path", "E:\\Temp\\");
System.out.println("\n\n\n");
System.out.println("After path:" + System.getProperty("java.class.path"));
URL url = SetClassPath.class.getResource("index.html"); // in E:\Temp
System.out.println("url=" + url);
url = SetClassPath.class.getResource("comm.jar"); // in E:\JavaDevelopment
System.out.println("url=" + url);
}
public static void main(String args[]) {
new SetClassPath();
System.exit(0);
}
}
-
hai,
The classpath is correctly changed.I am not sure how to access the resources, But I am sure that the class path is correctly changed and set in the environment.
To ensure this, I worked in Linux and tried to get the properties.
So I wrote the following program:
import java.io.*;
2 import java.net.*;
3
4 class Example1 {
5 public static void main(String args[]) {
6 System.out.println("Before path:" + System.getProperty("java.pat h"));
7 System.out.println(System.getProperties());
8 System.setProperty("java.class.path", "\\home\\rajasekhar");
9 System.out.println("\n\n\n");
10 System.out.println("After path:" + System.getProperty("java.clas s.path"));
11 try {
12 System.out.println(System.getProperties());
13 } catch (Exception e) { System.out.println("Error" +e );}
14 }
15 }
Please neglect the numeric numbers which represents the line numbers.
and the output I got is:
Before path:null
{java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=/usr/java/j2sdk1.4.2_04/jre/lib/i386, java.vm.version=1.4.2_04-b05, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=:, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io,
user.country=US, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/home/rajasekhar/rajasekhar/miscprograms, java.runtime.version=1.4.2_04-b05, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/usr/java/j2sdk1.4.2_04/jre/lib/endorsed, os.arch=i386, java.io.tmpdir=/tmp, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., os.name=Linux, sun.java2d.fontpath=, java.library.path=/usr/java/j2sdk1.4.2_04/jre/lib/i386/client:/usr/java/j2sdk1.4.2_04/jre/lib/i386:/usr/java/j2sdk1.4.2_04/jre/../lib/i386, java.specification.name=Java Platform API Specification, java.class.version=48.0, java.util.prefs.PreferencesFactory=java.util.prefs.FileSystemPreferencesFactory, os.version=2.4.20-8, user.home=/home/rajasekhar, user.timezone=, java.awt.printerjob=sun.print.PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.4,
java.class.path=., user.name=rajasekhar, java.vm.specification.version=1.0, java.home=/usr/java/j2sdk1.4.2_04/jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Sun Microsystems Inc., java.vm.info=mixed mode, java.version=1.4.2_04, java.ext.dirs=/usr/java/j2sdk1.4.2_04/jre/lib/ext, sun.boot.class.path=/usr/java/j2sdk1.4.2_04/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_04/jre/lib/i18n.jar:/usr/java/j2sdk1.4.2_04/jre/lib/sunrsasign.jar:/usr/java/j2sdk1.4.2_04/jre/lib/jsse.jar:/usr/java/j2sdk1.4.2_04/jre/lib/jce.jar:/usr/java/j2sdk1.4.2_04/jre/lib/charsets.jar:/usr/java/j2sdk1.4.2_04/jre/classes, java.vendor=Sun Microsystems
Inc., file.separator=/, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}
After path:\home\rajasekhar
{java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition, sun.boot.library.path=/usr/java/j2sdk1.4.2_04/jre/lib/i386, java.vm.version=1.4.2_04-b05, java.vm.vendor=Sun Microsystems Inc., java.vendor.url=http://java.sun.com/, path.separator=:, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io,
user.country=US, sun.os.patch.level=unknown, java.vm.specification.name=Java Virtual Machine Specification, user.dir=/home/rajasekhar/rajasekhar/miscprograms, java.runtime.version=1.4.2_04-b05, java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment, java.endorsed.dirs=/usr/java/j2sdk1.4.2_04/jre/lib/endorsed, os.arch=i386, java.io.tmpdir=/tmp, line.separator=
, java.vm.specification.vendor=Sun Microsystems Inc., os.name=Linux, sun.java2d.fontpath=, java.library.path=/usr/java/j2sdk1.4.2_04/jre/lib/i386/client:/usr/java/j2sdk1.4.2_04/jre/lib/i386:/usr/java/j2sdk1.4.2_04/jre/../lib/i386, java.specification.name=Java Platform API Specification, java.class.version=48.0, java.util.prefs.PreferencesFactory=java.util.prefs.FileSystemPreferencesFactory, os.version=2.4.20-8, user.home=/home/rajasekhar, user.timezone=, java.awt.printerjob=sun.print.PSPrinterJob, file.encoding=UTF-8, java.specification.version=1.4, java.class.path=\home\rajasekhar, user.name=rajasekhar, java.vm.specification.version=1.0, java.home=/usr/java/j2sdk1.4.2_04/jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Sun Microsystems Inc., java.vm.info=mixed mode, java.version=1.4.2_04, java.ext.dirs=/usr/java/j2sdk1.4.2_04/jre/lib/ext, sun.boot.class.path=/usr/java/j2sdk1.4.2_04/jre/lib/rt.jar:/usr/java/j2sdk1.4.2_04/jre/lib/i18n.jar:/usr/java/j2sdk1.4.2_04/jre/lib/sunrsasign.jar:/usr/java/j2sdk1.4.2_04/jre/lib/jsse.jar:/usr/java/j2sdk1.4.2_04/jre/lib/jce.jar:/usr/java/j2sdk1.4.2_04/jre/lib/charsets.jar:/usr/java/j2sdk1.4.2_04/jre/classes, java.vendor=Sun Microsystems Inc., file.separator=/, java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.cpu.isalist=}
I only pasted part of the output, which shows the classpath. So I am sure with the path that the classpath has been set. But To access a resource, I am not sure how to do it.
But in you program please try to acces the resource without using your own class file
for example
url = SetClassPath.class.getResource("comm.jar"); // in E:\JavaDevelopment
Please avoid using the class Name(SetClassPath in your case).
When you give the class, you are alternatively giving the path also.due to which it may not able to identify the correct resource.
Thanks
G.Rajasekhar
-
My point is that just displaying some paths might NOT mean that the JVM's classpath has been changed.
You need to demonstrate the the JVM's classpath has been changed. I thought that the getResource() method used the classpath to find resources. My code showed that a file on the classpath was found and that one NOT on the classpath was NOT found.
I thought that was the point of the original question. Changing the contents of a string doesn't solve the problem of how to change the classpath to be able to access resources/class files.
-
Well the solution gulpala that you are suggesting does'nt really work. Its displaying the modified classpath but when you try to access the class using Class.forname you would get NoClassDefFoundError.
Instead the approach i suggest here is use URLClassLoader.
Check the information provided in the forum: http://forum.java.sun.com/thread.jsp...rt=15&tstart=0
Below is the sample code suggested there dat is working appropriately.
public class ClassPathTest {
private static final Class[] parameters = new Class[] { URL.class };
public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}// end method
public static void addFile(File f) throws IOException {
addURL(f.toURL());
}// end method
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader) ClassLoader
.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[] { u });
} catch (Throwable t) {
t.printStackTrace();
throw new IOException(
"Error, could not add URL to system classloader");
}// end try catch
}// end method
public static void main(String[] args) throws Exception {
System.out.println("Hello.");
System.out.println("\nBefore ");
System.out.println("***********Test**************");
try {
Class.forName("com.test.Test");
System.out.println(" Test Class Found ....");
} catch (ClassNotFoundException e) {
System.out.println(" Test Class Not Found ....");
}
System.out.println("\n\n\n");
System.out.println("After");
try {
addFile("D:\\ClassPathTest\\lib\\test.jar");
Class.forName("com.test.Test");
System.out.println(" Test Class Found ....");
} catch (ClassNotFoundException e) {
System.out.println(" Test Class Found ....");
}
}
}
The output i got
-----------------------------------
Hello.
Before
***********Test**************
Test Class Not Found ....
After
***********Test **************
Test Class Found ....
-Priya
Similar Threads
-
Replies: 3
Last Post: 02-14-2005, 06:48 AM
-
Replies: 1
Last Post: 09-05-2002, 09:01 AM
-
By Robert Varga in forum .NET
Replies: 0
Last Post: 06-25-2002, 06:35 AM
-
Replies: 1
Last Post: 10-24-2000, 11:38 AM
-
Replies: 0
Last Post: 10-23-2000, 05:49 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|