| Author |
why not load class
|
chris liao
Greenhorn
Joined: Jul 24, 2005
Posts: 26
|
|
// why not load the righ class ?? please help me ! import java.io.*; import java.lang.*; import java.util.*; import java.net.*; import java.util.zip.*; public class Loader extends ClassLoader{ private List JarList = null; public Loader(){ JarList = new LinkedList(); } public Loader(ClassLoader parent) { super(parent); JarList = new LinkedList(); } public void addURL(ZipFile file){ if(file != null){ if(!JarList.contains(file)){ JarList.add(file); } } } public void removeURL(ZipFile file){ if(file != null){ if(JarList.contains(file)){ JarList.remove(file); } } } protected Class findClass(String name) throws ClassNotFoundException { try{ ZipEntry entry = getZipEntry(name); InputStream stream = getInputStream(entry); if(stream == null) throw new ClassNotFoundException(name); byte[] clsData = loadClassData(stream); Class c = defineClass(name,clsData,0,clsData.length); System.out.println("Class " + c); if (c == null) throw new ClassNotFoundException(name); return c; }catch(IOException e){ throw new ClassNotFoundException(name); } } private ZipEntry getZipEntry(String clsname){ String entryname = clsname.replace('.','/'); entryname = entryname +".class"; return new ZipEntry(entryname); } private InputStream getInputStream(ZipEntry entry){ Iterator it = JarList.iterator(); InputStream stream = null; while(it.hasNext()){ ZipFile file = (ZipFile)it.next(); try{ stream = file.getInputStream(entry); if(stream != null) break; }catch(IOException e){ } } return stream; } private byte[] loadClassData(InputStream stream)throws IOException { StringBuffer buffer = new StringBuffer(); while(true){ int byteValue = stream.read(); if(byteValue == -1) break; buffer.append(byteValue); } return buffer.toString().getBytes(); } public static void main(String args[]){ try{ ZipFile file = new ZipFile("e:\\j2ee.jar"); Loader loader = new Loader(); loader.addURL(file); System.out.println(loader.loadClass("javax.ejb.EJBHome")); }catch(Exception e){ e.printStackTrace(); } } }
|
 |
Marilyn de Queiroz
Sheriff
Joined: Jul 22, 2000
Posts: 9033
|
|
|
Custom ClassLoader stuff is waaaaay beyond beginner java. I'm moving this to Java in General( intermediate )
|
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
|
Why don't you tell us what happens when you try to run this program, and what you expected to happen instead?
|
[Jess in Action][AskingGoodQuestions]
|
 |
chris liao
Greenhorn
Joined: Jul 24, 2005
Posts: 26
|
|
|
O, When run, ClassFormatError is throwed !
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24054
|
|
Whoah! OK, now I actually looked at your code. This is not the way to read binary data like a class file -- you're totally corrupting the data! Since you don't always know the size of the decompressed data, the best way to do it is to use a ByteArrayOutputStream, doing something like:
|
 |
chris liao
Greenhorn
Joined: Jul 24, 2005
Posts: 26
|
|
|
Hand-mater, thanks for you hot help!
|
 |
 |
|
|
subject: why not load class
|
|
|