Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

setting up CLASS PATH dynamically

Thread view: 
strus_82 - 29 Mar 2008 17:07 GMT
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.
Arne Vajhøj - 29 Mar 2008 17:11 GMT
> I have a question related to CLASS PATH - is there a possibility to
> add some path to CLASS PATH during a runtime of application?
[quoted text clipped - 12 lines]
> 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
Roedy Green - 30 Mar 2008 08:22 GMT
On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
<marcin.strugala@gmail.com> 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
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

strus_82 - 30 Mar 2008 21:37 GMT
On 30 Mar, 09:22, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Sat, 29 Mar 2008 09:07:36 -0700 (PDT), strus_82
> <marcin.strug...@gmail.com> wrote, quoted or indirectly quoted someone
[quoted text clipped - 9 lines]
> 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.
Arne Vajhøj - 30 Mar 2008 22:19 GMT
> 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)
[quoted text clipped - 13 lines]
> 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
strus_82 - 30 Mar 2008 22:39 GMT
> It is much simpler than that !
>
[quoted text clipped - 4 lines]
>
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.