Hi,
Assume i have a .jar with a simple class "Simple.class" in it.
The jar lies in some directory, e.g. "~/myjar.jar"
I took a looked
here,
here and
here.
So i want simply to load any class from this myjar.jar
I dont really have a clue what i am doing wrong. There is always a ClassNotFoundException ...
|
Code:
|
public static void main(String[] args) throws Exception {
File file = new File(System.getProperty("user.home") + "/myjar.jar");
URLClassLoader clazzLoader = URLClassLoader.newInstance(new URL[]{file.toURI().toURL()});
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry element = entries.nextElement();
if (element.getName().endsWith(".class")) {
try {
Class c = clazzLoader.loadClass(element.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
} |
Put any simple jar file in home directory and this code snippet should run.
I my case it generates ClassNotFoundExceptions. What you think ?