Results 1 to 3 of 3
  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default [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:
    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;
    	}
    }
    Now I want to do:
    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();
    }
    I've allready searched at the internet for this, but I didn't found the right thing.

    Does someone know how I can do that?

    Thanks in advance,
    Dennis
    Last edited by Dennis; 02-14-2011 at 05:30 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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:

    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
                  . . .
             }
         }
    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.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 12:32 PM
  2. String Array to a Byte Array?
    By lazarus1410 in forum Advanced Java
    Replies: 1
    Last Post: 12-03-2010, 09:25 AM
  3. byte array.....
    By harsha.udupa2008 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 01:33 PM
  4. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  5. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •