First off I have seen Load Java-Byte-Code at Runtime and it was helpful in so far as getting to me to the same spot I'm stuck at currently.
I'm trying to load a class from a byte array to avoid storing a file on disk. For testing purpose in this example I am simply reading in a .class file into a byte array so obviously the file is still stored on disk, but it is just to see if the code can work.
I take this byte array and then use a custom ClassLoader with the method loadClass to load a Class, but it doesn't work.
byte[] bytearray = null;
try{
RandomAccessFile f = new RandomAccessFile("/sdcard/ClassToGet.dex", "r");
bytearray = new byte[(int) f.length()];
f.read(bytearray);
MyClassLoader classloader = new MyClassLoader();
classloader.setBuffer(bytearray);
classloader.loadClass("com.pack.ClassIWant");
}
Here is the ClassLoader implementation:
public class MyClassLoader extends DexClassLoader {
private byte[] buffer;
@Override
public Class findClass(String className){
byte[] b = getBuffer();
return this.defineClass(className, b, 0, b.length);
}
public void setBuffer(byte[] b){
buffer = b;
}
public byte[] getBuffer(){
return buffer;
}
And the error I am receiving is this:
java.lang.UnsupportedOperationException: can't load this type of class file at java.lang.VMClassLoader.defineClass(Native Method)
I have supplied it with .class files, .dex files, .apk, .jar, etc... I have no idea what "type of class file" it wants from me and the documentation on it is nonexistent. Any help would be great I've been trying to get this work for four days straight.