• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

why not load class

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
}
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Custom ClassLoader stuff is waaaaay beyond beginner java. I'm moving this to Java in General( intermediate )
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O, When run, ClassFormatError is throwed !
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hand-mater, thanks for you hot help!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic