0

I am wondering how I can load a byte array into a memory URLClassLoader? The byte array is the decrypted bytes of a jar file (as seen below)!

Most of the memory class loaders are using ClassLoader and not URLClassLoader! I need it to be using URLClassLoader.

    byte[] fileB = Util.crypt.getFileBytes(inputFile);
    byte[] dec;
    dec = Util.crypt.decrypt(fileB, "16LENGTHLONGKEYX".getBytes());
    //Load bytes into memory and load a class here?

Thanks!

1
  • You might have to stand on your head and create a URLStreamHandlerFactory and all the gorp behind it. Or else extend URLClassLoader so you can invoke defineClass. Commented Mar 23, 2012 at 1:15

2 Answers 2

2

Have you looked at the NetworkClassLoader example in the ClassLoader javadocs:

http://docs.oracle.com/javase/6/docs/api/index.html?java/lang/ClassLoader.html

Using this as a base, you just need to implement the loadClassData method, which will pull the desired resource from decrypted jar bytes. You can wrap the decrypted bytes with a JarInputStream(new ByteArrayInputStream(dec)), then iterate through the jar entries until you find the resource / class you're interested in and then return the jar entry's byte array

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, although I am not loading the jars directly from a network. All the jars are actually stored locally in the temp directory and downloaded every time you run the program. I am using "new URLClassLoader(jars)" (jars being an array list of all the jar files) to create the class loader, that seemed to work, although using a normal ClassLoader and adding them didn't.
I'm not saying that this will load it from a network, it's just some example code you can amend for your purposes. You can call your EncryptedJarClassLoader, add as many jars to an internal collection as you want and assuming you have a way of decrypting them, you should be able to follow the above
1

I will post here a implementation I had done in the past:

// main
String className = "tests.compiler.DynamicCompilationHelloWorldEtc";
//...
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
File classesDir = new File(tempDir);
CustomClassLoader ccl = new CustomClassLoader(classLoader, classesDir);         
if (ccl != null) {
    Class clazz = ccl.loadClass(className);
///...
}

My Custom ClassLoader:

package tests.classloader;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;

public class CustomClassLoader extends ClassLoader {

    private File classesDir;

    public CustomClassLoader(ClassLoader parent, File classesDir) {
       super(parent);      

       this.classesDir = classesDir;
    }

    public Class findClass(String name) {
       byte[] data = loadDataFromAny(name);
       return defineClass(name, data, 0, data.length);
    }

    private byte[] loadDataFromAny(String name) {

        name = name.replace('.', '/');
        name = name + ".class";

        byte[] ret = null;

        try {
            File f = new File(classesDir.getAbsolutePath(), name);
            FileInputStream fis = new FileInputStream(f);

            ByteBuffer bb = ByteBuffer.allocate(4*1024); 
            byte[] buf = new byte[1024];
            int readedBytes = -1; 

            while ((readedBytes = fis.read(buf)) != -1) {
                bb.put(buf, 0, readedBytes);
            }

            ret = bb.array();           
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        return ret;
    }
}

2 Comments

Let me try this and I'll get back to you! This may actually work.
I didn't post all the code, so probably you need to change/correct some lines. The original code contained a dynamic Java compiler that receives a String with code, compiles it and loads it using this ClassLoader.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.