1 year ago
#375736
code owl
How to set java classpath programatically for different directory at runtime?
Write two classes (say "A" and "B") , in class "A" create an instance of class "B". Compile class "A" in some directory "D1" and class "B" in "D2" , now A.class and B.class are in two separate directories. Try to run class "A" from directory "D1" (it will say class "B" not found). If you set classpath to both directories then it will work (try it out!) Try to set the classpath from the class "A" itself (instead of specifying the classpath in the DOS shell , try to set in in your java prog!).
Class A is in Directory 1;
public class A{
A(){
System.out.println("class A is called");
}
public static void main(String args[]){
System.setProperty("java.class.path", "D:/task2/Dir2");
B obj = new B();
}
}
Class B is in Directory 2
public class B{
B(){
System.out.println("class B is called");
}
public static void main(String args[]){
}
}
System.setProperty("java.class.path", "D:/task2/Dir2");
the above one doesn't work . Should i need to create my own classloader or any other way we have?
java
class
runtime
classpath
classloader
0 Answers
Your Answer