Results 1 to 3 of 3
- 02-14-2011, 04:23 PM #1
[ClassLoader] load byte array and use as jar
I found/create a program for loading jar files from an URL to the systemclassloader, then the object is always usable.
Now I want to load the jar file into a byte array and use that byte array as jar.
Current code, load directly from URL:
Now I want to do:Java Code:import java.awt.GridLayout; import java.awt.Panel; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; public class loader extends java.applet.Applet { private static final long serialVersionUID = 1L; private static final Class<?>[] parameters = new Class[]{URL.class}; public void init() { setLayout(new GridLayout()); try { addURL(new URL("http://localhost/theLibrary.jar")); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { final c c = new c("test", "panel"); if(c.getTheClass() instanceof Panel) add((Panel)c.getTheClass()); c.callMethod("setText", "hey"); } catch (Exception e) { System.out.println("error: " + e.getMessage()); } } private void addURL(URL u) throws IOException { URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class<?> sysclass = URLClassLoader.class; try { Method method = sysclass.getDeclaredMethod("addURL", parameters); method.setAccessible(true); method.invoke(sysloader, new Object[]{ u }); }catch(Throwable t){ t.printStackTrace(); throw new IOException("Error, could not add URL to system classloader"); } } } class c { private Object to; private Class<?> clazz; public c(String className, Object... args) throws Exception { clazz = ClassLoader.getSystemClassLoader().loadClass(className); Constructor<?> cs = clazz.getConstructor(getClassesFromObjects(args)); to = cs.newInstance(args); } public void callMethod(String methodName, Object... args) throws Exception { Method m = clazz.getMethod(methodName, getClassesFromObjects(args)); m.invoke(to, args); } private Class<?>[] getClassesFromObjects(Object... args) { Class<?>[] o = new Class<?>[0]; for(int i=0; i<args.length; i++) { Class<?>[] o2 = new Class<?>[o.length+1]; for(int j=0; j<o.length; j++) o2[j]=o[j]; o2[i]=args[i].getClass(); o=o2; } return o; } public Object getTheClass() { return to; } }
I've allready searched at the internet for this, but I didn't found the right thing.Java Code:try { URL u = new URL("http://localhost/theLibrary.jar") int contentLength = u.openConnection().getContentLength(); InputStream openStream = u.openStream(); byte[] binaryData = new byte[contentLength]; openStream.read(binaryData); openStream.close(); addJarToSystemClassLoader(binaryData); // unknow function } catch (IOException e) { e.printStackTrace(); }
Does someone know how I can do that?
Thanks in advance,
DennisLast edited by Dennis; 02-14-2011 at 04:30 PM.
- 02-14-2011, 06:45 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Write a tempfile (see the API docs for File) and load that with your URLClassLoader, if you don't want to use the jar over the network directly.
- 02-15-2011, 04:51 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
To my knowledge, there is no standard API feature to 'add a jar' to the classloader. However, it's pretty trivial to subclass ClassLoader and do this yourself. The API notes give the following example:
So what you would need to do, is to create a java.util.jar.JarFile from the bytes you read over the network, and then your findClass and loadClassData methods for your ClassLoader subclass would need to pull the proper JarEntry from the JarFile and return the appropriate byte[]/Class.Java Code:class NetworkClassLoader extends ClassLoader { String host; int port; public Class findClass(String name) { byte[] b = loadClassData(name); return defineClass(name, b, 0, b.length); } private byte[] loadClassData(String name) { // load the class data from the connection . . . } }
Similar Threads
-
convert byte array into char array
By kgkamaraj in forum New To JavaReplies: 4Last Post: 09-13-2011, 11:32 AM -
String Array to a Byte Array?
By lazarus1410 in forum Advanced JavaReplies: 1Last Post: 12-03-2010, 08:25 AM -
byte array.....
By harsha.udupa2008 in forum New To JavaReplies: 1Last Post: 04-27-2010, 12:33 PM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
Byte Array
By sandor in forum New To JavaReplies: 12Last Post: 01-15-2009, 03:31 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks