|
Friday, February 24, 2006 How to load subclass(es) from your jar file Mr. Endy Muhardin found problems in finding and loading classes when developing his opensource application. Hope this code will help him. Ganbatte Mas Endy!!Here is a simple code to illustrate how to load one or more subclass(es) from a jar file. /* * Created on Feb 24, 2006 * */ package com.monn; import java.io.File; import java.net.JarURLConnection; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import javax.swing.JFileChooser; import javax.swing.JOptionPane; /** * @author Monang Setyawan * */ public class Executor { public static void main(String[] args) throws Exception { new Executor().run(); System.exit(0); } public void run() throws Exception { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { String parent; if ((parent = JOptionPane.showInputDialog(null, "superclass/interface name")) != null) { File file = chooser.getSelectedFile(); URL jarUrl = new URL("jar:file:" + file.getAbsolutePath() + "!/"); JarFile jarFile = ((JarURLConnection) jarUrl.openConnection()) .getJarFile(); URL[] urls = { file.toURL() }; URLClassLoader loader = URLClassLoader.newInstance(urls, Thread .currentThread().getContextClassLoader()); List nameList = getClassNames(jarFile); for (Iterator iter = nameList.iterator(); iter.hasNext();) { String classname = (String) iter.next(); Class clazz = loader.loadClass(classname); if (Class.forName(parent).isAssignableFrom(clazz)) { // you've got your class System.out.println(clazz); } } } } } private List getClassNames(JarFile jarFile) { List nameList = new ArrayList(); for (Enumeration enum = jarFile.entries(); enum.hasMoreElements();) { JarEntry entry = (JarEntry) enum.nextElement(); String entryName = entry.getName(); if (entryName.endsWith(".class")) { int startPos = entryName.lastIndexOf('.'); String shortName = entryName.substring(0, startPos); shortName = shortName.replace('/', '.').replace('\\', '.'); nameList.add(shortName); } } return nameList; } } For simple test, you can select any JDBC driver jar file and type in "java.sql.Connection". My code above will try to find any implementations of Connection interface in your jar file. |
|
Cat Stevens said - If you want to sing out, sing out, and if you want to be free, be free, cause there's a million ways to be, you know that there are. - watashi likes... anime, games, Java, watching movies, operating illegal software and music downloads, playing the guitar, reading, football, cats dislikes... pineapples, snakes, Bush, Bill Gates, hypocrites valuable primates MartinFowler JamesGosling RickardOberg GradyBooch JasonHunter SteveJobs CedricBeust BruceTate HaniSulaiman DionAlmaer BruceEckel CarlosWhoever CameronPurdy GrahamGlass BillBurke GavinKing MarcFleury RichardStallman JamesStrachan ErikHatcher CraigMcClanahan MonsonHaefel GuidoVanRossum JimWaldo Joel Spolsky JackShirazi EricRaymond HeinzKabutz
archives 10/2004 other people
Mr. Good Indonesian donation provided by links behind the scene I like grey because it reminds me of the colour of my brain. My brain conjures up funny or useless thoughts to be ranted in this blog/journal.Let them speak This horrible page has been visited for times |
|
theWrittenOne
|
|
-Yet Another Useless Blog-
Random thought, Java, and anything |