• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Query on Dynamic class loading.

 
Greenhorn
Posts: 22
  • 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 load a class dynamically.
Please look at the following code:
String strFileName="C:\\Connector.jar";
String strClassName ="com/connector/connector";
strCurrClassPath = System.getProperty("java.class.path",".");
strNewClassPath = strCurrClassPath + ";"+strFileName;
System.setProperty("java.class.path",strNewClassPath);
String strNew = System.getProperty("java.class.path",".");
System.out.println("strPrevious is"+strNew);
ClassLoader c= new ClassFinder();
Class some = c.loadClass(strClassName);
strClassName is the class name available in strFileName.
But when i execute this code it is still giving ClassNotFoundException.
Please let me know the reason ?
This is a bit urgent.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JVM uses CLASSPATH of the system when it bootstraps,
so I suggest you do it before launching your JVM.
Anyway, what is ClassFinder ?
 
(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 an interesting way to change classpath at runtime: ClassPathHacker It uses reflection to violate the intended visibilty of methods, so it's a bit of a dirty hack - not sure I'd bet my job on it. I tried it to let me extend a system by adding configuration file references to the classpath and classees required. Works slick.
 
Author
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. Thats a great hack. Anyone who writes code to do that for anything other than testing/hacking/having fun should be shot (multiple times) but it's an interesting technique.

Hopefully the original poster doesn't really "need" to hack the classpath like this. I suspect he'd be doing just fine to create a new URLClassLoader and load the classes through it.
 
a wee bit from the empire
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic