• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Problem with dynamically setting the classpath at runtime

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
I am trying to make a small tool to find the class hierarchy of a class given the name of the class with the complete package structure.
I am using Class.forName(classname) to load the classes dynamically and am finding its superclass...
Am also giving an option to change the classpath.I am setting it by using the following command
System.setProperty("java.class.path", existingclasspath + ";" + newclasspath +";"); where neclasspath is a string that is taken fro the user at runtime.
Although this tachnique is changing the classpath env variable but stilll Class.forName id throwwing ClassNotFoundException.How do i go about it.Am giving the whole code....
.Please help.................
Source Code
--------------------------------------------------------------------------

Code tags added by Michael Morris.
Overly long lines fixed by Jim Yingst.
[ January 20, 2004: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pinjalim, welcome to JavaRanch and the crazy world of ClassLoaders and CLASSPATHs. Even though you dynamically change the CLASSPATH, the problem is that the ClassLoader(s) idea of the CLASSPATH does not change so it is unable to find or resolve a class or dependent class outside of its notion of the CLASSPATH. You could try writing your own ClassLoader to overcome this problem, but aside from that, I don't think you will have much luck.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have to write your own ClassLoader, but you will have to create your own instance of a ClassLoader. THis ClassLoader will know about the extra Class location and will be able to load and return the Class.
I have some sample code for this, I'll have a look for it...
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to edit the sample. the code I had did something else entirely. It may not work without some

This code adds the URL to the Class location, then it creates a ClassLOader that knows where this location is, then it uses that ClassLoader to load the CLass in that location.
Hope this is what you were looking for.
Dave.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a solution using reflection to get to protected features of the class loader. Probably not something to bet your career on:

From : http://forum.java.sun.com/thread.jsp?forum=32&thread=300557&message=1191210
[ January 19, 2004: Message edited by: Michael Morris ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This code adds the URL to the Class location, then it creates a ClassLOader that knows where this location is, then it uses that ClassLoader to load the CLass in that location.


You could still throw a ClassNotFoundException if the class you are attempting to load is dependent on a class loaded by some other ClassLoader that your URLClassLoader knows nothing about.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops. Actually you won't get a ClassNotFoundException, but if you try to call newInstance on the loaded class you can get a NoClassDefFoundError. Try this:
Put this in the ClassLoaderTests directory:

Put this in the ClassLoaderTests2 directory:

Put this in the TestMain directory:

The results should be something like this:
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, I was assuming there was just the default loader and the URLClassLoader. In my case, the added path would only be able to see itself and the 'java.**' classes. But in your case, you would include 'ClassLoaderTests' and 'ClassLoaderTests2' in the same ClassLoader and it should work fine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic