Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > setting up CLASS PATH dynamically

Reply
Thread Tools

setting up CLASS PATH dynamically

 
 
strus_82
Guest
Posts: n/a
 
      03-29-2008
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.
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-29-2008
strus_82 wrote:
> 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      03-30-2008
On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who 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?


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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
strus_82
Guest
Posts: n/a
 
      03-30-2008
On 30 Mar, 09:22, Roedy Green <(E-Mail Removed)>
wrote:
> On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
> <(E-Mail Removed)> wrote, quoted or indirectly quoted someone
> who 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?

>
> you can fire up a new classloader with a different classpath.
> seehttp://mindprod.com/jgloss/classloader.html
> --
>
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com


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.
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      03-30-2008
strus_82 wrote:
> 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
 
Reply With Quote
 
strus_82
Guest
Posts: n/a
 
      03-30-2008

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting 'Path' in Windows XP (and path length) DW Computer Support 7 01-03-2011 07:29 PM
library.path VS class.path Terracotta Java 1 07-16-2007 03:52 PM
Dynamically setting path for include files Dave ASP General 1 12-05-2005 09:30 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Absolute path in Manifest Class-Path header? bo_kid@yahoo.com Java 10 05-30-2005 01:43 PM



Advertisments