Results 1 to 4 of 4
  1. #1
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default 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 ...

    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();
    				}
    			}
    		}
    	}
    Put any simple jar file in home directory and this code snippet should run.

    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 ?"

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    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.

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    thanks, you are right.
    i didn't read carefully about the "binary name" parameter.

    so now i got this working solution
    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();
    				}
    			}
    		}
    	}
    but is there a better way? How can i acquire a resource as a class ?
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Uhm, as soon as you have the urlclassloader simply do
    Java Code:
    Class.forName(<className>, <classLoader>)

Similar Threads

  1. Dynamic image loading in jsp
    By ramakrishna k m in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 10-10-2011, 06:08 AM
  2. Run external programs using runtime class
    By JBird in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 02:37 AM
  3. class loading
    By purejoker in forum AWT / Swing
    Replies: 1
    Last Post: 01-20-2009, 01:09 PM
  4. Problems adding an external class
    By jan2321 in forum Eclipse
    Replies: 2
    Last Post: 11-06-2008, 11:34 AM
  5. External JavaScript files not loading
    By sajut in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 06:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •