Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 12:44 PM
AndreB's Avatar
Senior Member
 
Join Date: Dec 2009
Location: Stuttgart, Germany
Posts: 105
Rep Power: 0
AndreB is on a distinguished road
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 ...

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 ?"
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-08-2010, 01:02 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2010, 01:28 PM
AndreB's Avatar
Senior Member
 
Join Date: Dec 2009
Location: Stuttgart, Germany
Posts: 105
Rep Power: 0
AndreB is on a distinguished road
Default
thanks, you are right.
i didn't read carefully about the "binary name" parameter.

so now i got this working solution
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 ?"
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-08-2010, 01:55 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,622
Rep Power: 4
masijade is on a distinguished road
Default
Uhm, as soon as you have the urlclassloader simply do
Code:
Class.forName(<className>, <classLoader>)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 08:43 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org