Sagewire Logo

changing the classpath at runtime in code

13 Message(s) by 4 Author(s) originally posted in java programmer


From: Aryeh M. Friedman Date:   Wednesday, October 24, 2007
Is it possible for a JAVA app to change it's class path at runtime. I
tried:

System.setProperty("JAVA.class.path","foo")

The reason for wanting to try is URL ClassLoader won't physically
reread a class file if it is in the classpath. See
http://groups.google.com/group/comp.lang.JAVA.program mer/browse_thread /thread/793a9146df18365a/ceaa154348f6f271#ceaa154348f6f271


From: =?ISO-8859-1?Q?Arne_Vajh=F8j?= Date:   Wednesday, October 24, 2007
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.



Create a URLClassLoader with a url that are *not* in the parent
classload ers url's.

Arne


From: Aryeh M. Friedman Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
> Is it possible for a JAVA app to change it's classpath at runtime. I
> tried:
> System.setProperty("JAVA.class.path","foo")
> The reason for wanting to try is URLClassLoader won't physically
> reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.



so just do:

URL[] url=3D{new URL("file://foo")};
URLClassLoader laoder=3Dnew URLClassLoader(url);
?


From: =?ISO-8859-1?Q?Arne_Vajh=F8j?= Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
so just do:
URL[] url={new URL("file://foo")};
URLClassLoader laoder=new URLClassLoader(url);
?



I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).

Arne

===============================

import JAVA.io.*;
import JAVA.net.*;

public class DoubleDynmaic {
private static void dynno(int n) {
(new File("test")).mkdir();
try {
OutputStream os = new FileOutputStream("test/Test.JAVA");
PrintStream ps = new PrintStream(os);
ps.println("public class Test {");
ps.println(" public Test() {");
ps.println(" System.out.println(" + n + ");");
ps.println(" }");
ps.println("}");
ps.close();
os.close();
Runtime.getRuntime().exec("JAVAc -d test
test/Test.JAVA").waitFor();
URL[] url = new URL[1];
url[0] = new URL("file:test/");
URLClassLoader cl = new URLClassLoader(url);
Class.forName("Test", true, cl).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int I = 0; I < 10; i++) {
dynno(i);
}
}
}


From: Aryeh M. Friedman Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
> Is it possible for a JAVA app to change it's classpath at runtime. I
> tried:
> System.setProperty("JAVA.class.path","foo")
> The reason for wanting to try is URLClassLoader won't physically
> reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
> so just do:
> URL[] url=3D{new URL("file://foo")};
> URLClassLoader laoder=3Dnew URLClassLoader(url);
> ?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).



You posted this in the original thread but as noted in the linked to
thread almost all the classes I am dealing with *WILL* be in the
classpath.... so the question remains if I do the code in my previous
post will it now load from the class path or from "foo".

Arne
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D
import JAVA.io.*;
import JAVA.net.*;
public class DoubleDynmaic {
private static void dynno(int n) {
(new File("test")).mkdir();
try {
OutputStream os =3D new FileOutputStream("test/Test.JAVA");
PrintStream ps =3D new PrintStream(os);
ps.println("public class Test {");
ps.println(" public Test() {");
ps.println(" System.out.println(" + n + ");");
ps.println(" }");
ps.println("}");
ps.close();
os.close();
Runtime.getRuntime().exec("JAVAc -d test
test/Test.JAVA").waitFor();
URL[] url =3D new URL[1];
url[0] =3D new URL("file:test/");
URLClassLoader cl =3D new URLClassLoader(url);
Class.forName("Test", true, cl).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int I =3D 0; i < 10; i++) {
dynno(i);
}
}


> }


From: Aryeh M. Friedman Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
> Is it possible for a JAVA app to change it's classpath at runtime. I
> tried:
> System.setProperty("JAVA.class.path","foo")
> The reason for wanting to try is URLClassLoader won't physically
> reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
> so just do:
> URL[] url=3D{new URL("file://foo")};
> URLClassLoader laoder=3Dnew URLClassLoader(url);
> ?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).



Perhaps google ate my previous response.... but as I said several
places (including the linked to thread) almost all my classes *WILL*
be in the classpath... btw as mentioned in an other thread your demo
does not quite work once divorced from the code to rewrite the class
(see orginial thread)... so the question remains does the code above
load from "foo" or from the classpath when I do loader.loadClass(...)


From: =?ISO-8859-1?Q?Arne_Vajh=F8j?= Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
so just do:
URL[] url={new URL("file://foo")};
URLClassLoader laoder=new URLClassLoader(url);
?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).
You posted this in the original thread but as noted in the linked to
thread almost all the classes I am dealing with *WILL* be in the
classpath.... so the question remains if I do the code in my previous
post will it now load from the class path or from "foo".



If the class get loaded by the parent classloader because it is
in the apps general classpath, then you can (as far as I know)
neither get it unloaded or reloaded.

You need to get those classes of the classpath.

Arne


From: Daniel Pitts Date:   Wednesday, October 24, 2007
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath. See
http://groups.google.com/group/comp.lang.JAVA.programmer/browse_thread/thread/793a9146df18365a/ceaa154348f6f271#ceaa154348f6f271


Once a class is loaded by any class loader, it can not be reloaded by
any other class loader in that same branch of the class-loader tree.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


From: Daniel Pitts Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
so just do:
URL[] url={new URL("file://foo")};
URLClassLoader laoder=new URLClassLoader(url);
?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).
Perhaps google ate my previous response.... but as I said several
places (including the linked to thread) almost all my classes *WILL*
be in the classpath... btw as mentioned in an other thread your demo
does not quite work once divorced from the code to rewrite the class
(see orginial thread)... so the question remains does the code above
load from "foo" or from the classpath when I do loader.loadClass(...)


Here's the scoop.
If its ever on the class path, you can not reload it. What you're trying
to do isn't possible to do without restricting what's on the class
path. That isn't a bad restriction! Just make it happen.

Changing the class path after the fact is not going to help either.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


From: Aryeh M. Friedman Date:   Wednesday, October 24, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
> Is it possible for a JAVA app to change it's classpath at runtime. =
I
> tried:
> System.setProperty("JAVA.class.path","foo")
> The reason for wanting to try is URLClassLoader won't physically
> reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
> so just do:
> URL[] url=3D{new URL("file://foo")};
> URLClassLoader laoder=3Dnew URLClassLoader(url);
> ?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).
> You posted this in the original thread but as noted in the linked to
> thread almost all the classes I am dealing with *WILL* be in the
> classpath.... so the question remains if I do the code in my previous
> post will it now load from the class path or from "foo".
If the class get loaded by the parent classloader because it is
in the apps general classpath, then you can (as far as I know)
neither get it unloaded or reloaded.
You need to get those classes of the classpath.



Since this is for a commercial unit testing product this isn't
possible in practice (it isn't safe to make assumptions about where
people place things). Some one off line suggested forking a process
to do it.... if I can not find a ClassLoader based solution I'll have
to do that but it is down right a) not portable b) evil


From: Daniel Pitts Date:   Thursday, October 25, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
so just do:
URL[] url={new URL("file://foo")};
URLClassLoader laoder=new URLClassLoader(url);
?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).
You posted this in the original thread but as noted in the linked to
thread almost all the classes I am dealing with *WILL* be in the
classpath.... so the question remains if I do the code in my previous
post will it now load from the class path or from "foo".
If the class get loaded by the parent classloader because it is
in the apps general classpath, then you can (as far as I know)
neither get it unloaded or reloaded.

You need to get those classes of the classpath.
Since this is for a commercial unit testing product this isn't
possible in practice (it isn't safe to make assumptions about where
people place things). Some one off line suggested forking a process
to do it.... if I can not find a ClassLoader based solution I'll have
to do that but it is down right a) not portable b) evil


Hmm, I think as a publisher of said software, you can say how it should
work. If you can not make any assumptions whatsoever then you're lost.
You will end up writing this program for clients who want to be able to
bounce a network signal off the moon for some reason.

It's all about making reasonable assumptions. It is reasonable to
assume that you can have a degree of control the environment/context in
which your application starts up. Document the fact that the code to be
tested can not be in the same path as your product.

Also, you seem to make a big deal about this code being "commercial".
This rule applies for any publicly distributed product, whether
commercial or otherwise.

Honestly, I think you're going down the wrong road with this. Have you
looked at other existing test frameworks? What is yours going to offer
that, say, JUnit does not?
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


From: Roedy Green Date:   Thursday, October 25, 2007
On Wed, 24 Oct 2007 23:38:10 -0000, "Aryeh M. Friedman"
wrote in message, quoted or indirectly quoted someone
who said :

Is it possible for a JAVA app to change it's classpath at runtime



yes but it requires firing up a new classloader.

See http://mindprod.com/jgloss/classloader.html
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: =?ISO-8859-1?Q?Arne_Vajh=F8j?= Date:   Friday, October 26, 2007
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
wrote in message:
Is it possible for a JAVA app to change it's classpath at runtime. I
tried:
System.setProperty("JAVA.class.path","foo")
The reason for wanting to try is URLClassLoader won't physically
reread a class file if it is in the classpath.
Create a URLClassLoader with a url that are *not* in the parent
classloaders url's.
so just do:
URL[] url={new URL("file://foo")};
URLClassLoader laoder=new URLClassLoader(url);
?
I have a little demo example I often use to illustrate
(subdir test isn't in classpath of program).
You posted this in the original thread but as noted in the linked to
thread almost all the classes I am dealing with *WILL* be in the
classpath.... so the question remains if I do the code in my previous
post will it now load from the class path or from "foo".
If the class get loaded by the parent classloader because it is
in the apps general classpath, then you can (as far as I know)
neither get it unloaded or reloaded.

You need to get those classes of the classpath.
Since this is for a commercial unit testing product this isn't
possible in practice (it isn't safe to make assumptions about where
people place things). Some one off line suggested forking a process
to do it.... if I can not find a ClassLoader based solution I'll have
to do that but it is down right a) not portable b) evil



You don't have any control over where people put their stuff,
but you've control over your stuff.

If the only thing in classpath is your jar file, then you can
create your own classloader for their stuff and unload and reload
as needed.

Arne



Next Message: Startup application


Blogs related to changing the classpath at runtime in code

Re: *SPAM*: 06.40/6.0 - Re: How use org.dom4j in my tuscany-sca ...
You should make the > > > new jar available on the runtime classpath alongside the compile > > > component implementations. Take the run-classes target > > > > > > > > > <java classname="${test.class}" ...

JAVA BY MOHIT
As a result, Microsoft no longer ships Java with Windows, and in recent versions of Windows, Internet Explorer cannot support Java applets without a third-party plugin. However, Sun and others have made available Java run-time systems ...

Java Class Loaders
Class loaders have always been the key component of the Java, loading classes into the JVM at runtime. Class loaders fetch classes into the JVM, so they constitute the first line of defense in the JVM Sandbox, filtering malicious code, ...

Tips & Techniques for Java
The specification for the Java programming language and the Java Virtual Machine (the byte code interpreter) is open and has been made publicly available by Sun. Sun is also licensing the development and runtime software, and makes the ...

How to install Java
In addition to setting up the path, you also need to tell Java where to find compiled class files at runtime. You will probably want to set the classpath to include at least the current working directory (.) ...

Runtime
[1] A vulnerability in the Java Runtime Environment (JRE) may allow malicious Javascript code that is downloaded by a browser from a malicious website to make network connections, through Java APIs, to network services on machines other ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional