Results 1 to 4 of 4
- 02-08-2010, 12:44 PM #1
Dynamic Class Loading from external Jar
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 ...
Put any simple jar file in home directory and this code snippet should run.Java 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(); } } } }
I my case it generates ClassNotFoundExceptions. What you think ?"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 02-08-2010, 01:02 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Does "element.getName()" return the entire package path?
I.E. a.b.c.hello (without the ".class" extension, of course, but including the entire package "." separated and not "/" separated as defined by the API docs)
Somehow, I don't believe so.
- 02-08-2010, 01:28 PM #3
thanks, you are right.
i didn't read carefully about the "binary name" parameter.
so now i got this working solution
but is there a better way? How can i acquire a resource as a class ?Java 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()}); // System.class.getClassLoader() 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().replaceAll(".class", "").replaceAll("/", ".")); } catch (Exception e) { e.printStackTrace(); } } } }"There is no foolproof thing; fools are too smart."
"Why can't you solve my Problem ?"
- 02-08-2010, 01:55 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Similar Threads
-
Dynamic image loading in jsp
By ramakrishna k m in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 10-10-2011, 06:08 AM -
Run external programs using runtime class
By JBird in forum New To JavaReplies: 3Last Post: 08-06-2009, 02:37 AM -
class loading
By purejoker in forum AWT / SwingReplies: 1Last Post: 01-20-2009, 12:09 PM -
Problems adding an external class
By jan2321 in forum EclipseReplies: 2Last Post: 11-06-2008, 10:34 AM -
External JavaScript files not loading
By sajut in forum New To JavaReplies: 0Last Post: 02-15-2008, 05:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks