Reputation: 41
I have a .class file as(JJJ.class) at location C:\Users\user\Desktop\jk ... i want to load dynamically this .class file in java application.here i want to load .class file dynamically in the project
package com.load.data;
public class JJJ {
public static void main(String[] args)throws ClassNotFoundException,MalformedURLException
{
File file = new File("C:\\Users\\user\\Desktop\\jk");
URL url = file.toURL();
URL[] urls = new URL[]{url};
ClassLoader cl = new URLClassLoader(urls);
Class cls = cl.loadClass("com.load.data.JJJ");
}
}
Upvotes: 1
Views: 993
Reputation: 999
You better use your JJJ class as the class loader and check that the class you try to load is in the classpath.
public class JJJ {
public static void main(String[] args){
ClassLoader classLoader = JJJ.class.getClassLoader();
try {
Class aClass = classLoader.loadClass("com.load.data.JJJ");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 43738
The package structure must be reflected in the directory structure. If the class loader loads from C:\Users\user\\Desktop\jk
the class must actually be stored here:
C:\Users\user\Desktop\jk\com\load\data\JJJ.class
Upvotes: 2