ClassLoader cl = new ClassLoader() {
protected Class<?> findClass(String name) throws ClassNotFoundException
{
try
{
JarInputStream inputStream = new JarInputStream(new FileInputStream("C:/test.jar"));
for(ZipEntry entry; (entry = inputStream.getNextEntry()) != null;)
{
String entryName = entry.toString().replace('.', '/');
if(!name.equals(entryName))
continue;
byte[] bytes = new byte[Integer.MAX_VALUE];
int len = inputStream.read(bytes, 0, Integer.MAX_VALUE);
return(defineClass(name, bytes, 0, len));
}
}
catch (IOException e)
{
e.printStackTrace();
}
throw new ClassNotFoundException("Classn't found");
}
};
try
{
Class<?> c= cl.loadClass("main/Testclass.class");
c.newInstance();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
ClassLoader cl = new ClassLoader() {
protected Class<?> findClass(String name) throws ClassNotFoundException
{
try
{
JarInputStream inputStream = new JarInputStream(bais);
for(ZipEntry entry; (entry = inputStream.getNextJarEntry()) != null;)
{
String entryName = entry.toString().replace('/', '.');
if(!name.equals(entryName))
continue;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(int len = 0; len != -1;)
{
byte[] cache = new byte[1024];
len = inputStream.read(cache, 0, cache.length);
baos.write(cache);
}
byte[] bytes = baos.toByteArray();
return(defineClass(name, bytes, 0, bytes.length));
}
}
catch (IOException e)
{
e.printStackTrace();
}
throw new ClassNotFoundException("Classn't found");
}
};
/**
* Reads the data from the given inputStream and returns it as
* a byte array.
*
* @param inputStream The inputStream to read
* @return The data from the inputStream
*/
public static byte[] readData(InputStream inputStream)
{
ByteArrayOutputStream baos = null;
try
{
baos = new ByteArrayOutputStream();
byte buffer[] = new byte[8192];
while (true)
{
int read = inputStream.read(buffer);
if (read == -1)
{
break;
}
baos.write(buffer, 0, read);
}
baos.flush();
return baos.toByteArray();
}
catch (IOException e)
{
throw new RuntimeException("Could not load data", e);
}
finally
{
if (baos != null)
{
try
{
baos.close();
}
catch (IOException e)
{
throw new RuntimeException(
"Could not close output", e);
}
}
}
}
ClassLoader cl = new ClassLoader() {
protected Class<?> findClass(String name) throws ClassNotFoundException
{
Class<?> requestClass = null;
try
{
JarInputStream inputStream = new JarInputStream(bais);
for(ZipEntry entry; (entry = inputStream.getNextJarEntry()) != null;)
{
String entryName = entry.toString().replace('/', '.');
if(entryName.endsWith(".class"))
entryName = entryName.substring(0, entryName.length() - ".class".length());
byte[] bytes = readClass(inputStream);
if(!name.contains(entryName))
defineClass(entryName, bytes, 0, bytes.length);
else
requestClass = defineClass(entryName, bytes, 0, bytes.length);
}
}
catch (IOException e)
{
e.printStackTrace();
}
if(requestClass == null)
throw new ClassNotFoundException("Classn't found");
return(requestClass);
}
private byte[] readClass(InputStream inputStream) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] cache = new byte[1024 * 16];
for(int len = 0; len != -1;)
{
len = inputStream.read(cache, 0, cache.length);
if(len != -1)
baos.write(cache, 0, len);
}
return(baos.toByteArray());
}
};
package at.kai.logic;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
class StreamClassLoader extends ClassLoader
{
private InputStream source;
StreamClassLoader(InputStream is)
{
super();
source = is;
}
StreamClassLoader(byte[] b)
{
super();
source = new ByteArrayInputStream(b);
}
protected Class<? extends Object> findClass(String name) throws ClassNotFoundException
{
Class<? extends Object> requestClass = null;
JarInputStream jis = null;
try
{
boolean anyClassNoDef;
do
{
anyClassNoDef = false;
jis = new JarInputStream(source);
for(ZipEntry entry; (entry = jis.getNextEntry()) != null;)
{
String entryName = conformEntryname(entry.getName());
byte[] bytes = readClass(jis);
try
{
if(!name.contains(entryName))
defineClass(entryName, bytes, 0, bytes.length);
else
requestClass = defineClass(entryName, bytes, 0, bytes.length);
}
catch (ClassFormatError e) {}
catch (NoClassDefFoundError e)
{
anyClassNoDef = true;
}
}
jis.close();
}
while(anyClassNoDef);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if(jis != null)
jis.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if(requestClass == null)
throw new ClassNotFoundException("Classn't found");
return(requestClass);
}
private String conformEntryname(String name)
{
name = name.replace('/', '.');
if(name.endsWith(".class"))
name = name.substring(0, name.length() - ".class".length());
return(name);
}
private byte[] readClass(InputStream inputStream) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] cache = new byte[1024 * 16];
for(int len = 0; len != -1;)
{
len = inputStream.read(cache, 0, cache.length);
if(len != -1)
baos.write(cache, 0, len);
}
return(baos.toByteArray());
}
}