hab da grad noch was auf der Platte...sollte eigentlich funktionieren..
aber ?? Keine Garantie...lang ists her...
[HIGHLIGHT="Java"]import com.sun.org.apache.bcel.internal.classfile.ClassFormatException;
public class JarClassLoader extends ClassLoader {
private JarResource jarResource;
public JarClassLoader(String jarName) {
jarResource = new JarResource(jarName);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
return loadClass(name, true);
}
@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class<?> result;
// ist vielleicht schon im CP
try {
result = super.findSystemClass(name);
System.out.println("Klasse im Classpath gefunden");
return result;
} catch (ClassNotFoundException e) {
System.out.println("Klasse nicht im Classpath");
}
// lade von der Resource
byte[] classBytes = jarResource.getResource(formatClassUrl(name));
if (classBytes == null) {
throw new ClassNotFoundException("Klasse nicht in der jar");
}
result = defineClass(name, classBytes, 0, classBytes.length);
if (result == null) {
throw new ClassFormatException();
}
if (resolve) {
resolveClass(result);
}
return result;
}
private static final String formatClassUrl(String jarName) {
String result = jarName.replace('.', '/');
return result + ".class";
}
public static void main(String[] args) throws ClassNotFoundException, Exception,
IllegalAccessException {
JarClassLoader loader = new JarClassLoader(
"/main/Development/sourcen/spring-framework-2.5.6/lib/dom4j/dom4j-1.6.1.jar");
Class<?> clazz = loader.loadClass("org.dom4j.XPath");
Class<?> clazz2 = loader.loadClass("org.dom4j.util.NodeComparator");
System.out.println(clazz);
System.out.println(clazz2);
Object nc = clazz2.newInstance();
System.out.println(nc);
}
}
[/HIGHLIGHT]
[HIGHLIGHT="Java"]import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
public final class JarResource {
private Map<String, Long> classSizes = new HashMap<String, Long>();
private Map<String, byte[]> jarContent = new HashMap<String, byte[]>();
private String jarFileName;
public JarResource(String jarFileName) {
this.jarFileName = jarFileName;
init();
}
public byte[] getResource(String name) {
return jarContent.get(name);
}
private void init() {
try {
File file = new File(jarFileName);
if (!file.exists()) {
throw new FileNotFoundException();
}
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry entry = enu.nextElement();
classSizes.put(entry.getName(), entry.getSize());
}
jarFile.close();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
JarInputStream jis = new JarInputStream(bis);
JarEntry entry = null;
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
long size = entry.getSize();
if (size < 0) {
size = classSizes.get(entry.getName());
}
byte[] buffer = new byte[(int) size];
int offset = 0;
while ((size - offset) > 0) {
int chunk = jis.read(buffer, offset, (int) size - offset);
if (chunk == -1) {
break;
}
offset += chunk;
}
jarContent.put(entry.getName(), buffer);
}
} catch (NullPointerException e) {
System.out.println("done.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[/HIGHLIGHT]