Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not load class

 
chris liao
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom ClassLoader stuff is waaaaay beyond beginner java. I'm moving this to Java in General( intermediate )
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you tell us what happens when you try to run this program, and what you expected to happen instead?
 
chris liao
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O, When run, ClassFormatError is throwed !
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hand-mater, thanks for you hot help!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic