• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in setting the classpath dynamically

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am setting the classpath thru the code. It is setting the classpath but the class for which classpath is ser is not accessible.
could u please look into the code :
import java.io.*;
import java.net.*;
import java.util.*;
public class Testing1{
public static void main(String args[]){

Class className = null;
String sClassName = "f:\\TestingToolPackage";
try{
Properties strPrev = System.getProperties(); strPrev.setProperty("java.class.path",sClassName);
System.out.println(strPrev.getProperty("java.class.path").toString());

URL[] urls = new URL[1];
urls[0] = new URL
( "file://f:/TestingToolPackage" );
ClassLoader cl = new URLClassLoader( urls );
className = cl.loadClass
( "TestingTool.TestPrograms.Triangle" );

if(className == null){
System.out.println(" Things did not work");
}else{
System.out.println(" It worked "+className);
java.lang.reflect.Method methods[] = className.getDeclaredMethods();
for(int i=0; i < methods.length; i++)
System.out.println("in gui "+methods[i].getName());
}
}catch(Exception e){
System.out.println("While trying to get an instance of the class "+e);
e.printStackTrace();
}
}
}
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add another slash / character to the end of the
URL String, i.e.,
file://f:/TestingToolPackage/
and it should find any class in that directory or
below.
HTH,
Joe
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can set it in your code, even you can read it back, but you can never really change classpath recognized by JVM!!!
Only remembering this will save you a lot of useless and endless effort.
However, if you don't believe it, then work hard to prove I'm wrong! One counter example will be enough... I'll be more than happy to see it...
Visit JavaChina on the web
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you can use URLClassLoader to load classes not on your classpath. However, that has nothing to do with Setting classpath dynamically.
That is a totally different topic.
 
A tiny monkey bit me and I got tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic