setting up CLASS PATH dynamically

S

strus_82

Hello,
I have a question related to CLASS PATH - is there a possibility to
add some path to CLASS PATH during a runtime of application?

I've tried the following:

Properties properties = System.getProperties();
String key = "java.class.path";
String value = "d:\\some\\new\\path\\;" + properties.getProperty(key);
System.setProperty("java.class.path", value);

and then using a reflection mechanism I've tried to execute some
method but it doesn't work properly (seems like the class path is not
updated).

When I set the CLASS PATH during the execution of program the method
works fine.


thanks in advance,
M.
 
A

Arne Vajhøj

strus_82 said:
I have a question related to CLASS PATH - is there a possibility to
add some path to CLASS PATH during a runtime of application?

I've tried the following:

Properties properties = System.getProperties();
String key = "java.class.path";
String value = "d:\\some\\new\\path\\;" + properties.getProperty(key);
System.setProperty("java.class.path", value);

and then using a reflection mechanism I've tried to execute some
method but it doesn't work properly (seems like the class path is not
updated).

When I set the CLASS PATH during the execution of program the method
works fine.

I don't think so.

Create an URLClassLoader with the classpath you need and use
that to load the classes.

Arne
 
S

strus_82

you can fire up a new classloader with a different classpath.
seehttp://mindprod.com/jgloss/classloader.html

Hello,
I'm trying to write my ClassLoader but I have the following problem:
when I read all data from .class (here it is CustomClassLoader.class)
file to byte array - I call defineClass() but an exception is thrown:
"d:/examples/temp/CustomClassLoader (wrong name: CustomClassLoader)".

I think I pass wrong first parameter to defineClass(String name,
byte[] b, int off, int len). The CustomClassLoader.class file is
located in d:\examples\temp\.

I have also tried to use findClass() but it's the same.

I have tried different combination of the path to that .class file but
the result is the same all the time :/

Could You give me some hint? All I want to do (for couple hours :/) is
to load class from some path of the local file system (but the path is
not set in CLASS PATH).

Thanks in advance,
M.
 
A

Arne Vajhøj

strus_82 said:
I'm trying to write my ClassLoader but I have the following problem:
when I read all data from .class (here it is CustomClassLoader.class)
file to byte array - I call defineClass() but an exception is thrown:
"d:/examples/temp/CustomClassLoader (wrong name: CustomClassLoader)".

I think I pass wrong first parameter to defineClass(String name,
byte[] b, int off, int len). The CustomClassLoader.class file is
located in d:\examples\temp\.

I have also tried to use findClass() but it's the same.

I have tried different combination of the path to that .class file but
the result is the same all the time :/

Could You give me some hint? All I want to do (for couple hours :/) is
to load class from some path of the local file system (but the path is
not set in CLASS PATH).

It is much simpler than that !

URLClassLoader cl = new URLClassLoader("file:/d:/examples/temp/");
Object o = Class.forName("mypackage.MyClass", true, cl).newInstance();

will load mypackage.MyClass from D:\examples\temp\mypackage\MyClass.class !

Arne
 
S

strus_82

It is much simpler than that !

URLClassLoader cl = new URLClassLoader("file:/d:/examples/temp/");
Object o = Class.forName("mypackage.MyClass", true, cl).newInstance();

will load mypackage.MyClass from D:\examples\temp\mypackage\MyClass.class !

Arne

Indeed :) thx. I've tried this but I used toURL() as a argument to
URLClassLoader (it takes URL[]) - but this toURL() is deprecated
starting from v.1.5, bur as You wrote I can use simply a string:
URLClassLoader(new URL[] {new URL("file:/d:/examples/temp/")});

But could you tell me what I did wrong in case of defining my
ClassLoader?

Best regards,
M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top