Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:17:14+00:00 2026-06-09T03:17:14+00:00

First off I have seen Load Java-Byte-Code at Runtime and it was helpful in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:17:15+00:00Added an answer on June 9, 2026 at 3:17 am

    Make sure your .dex file is a genuine dx-produced Dalvik executable and not a Java .class file in disguise. If you use the .dex extension the file must be a .dex file; otherwise use a .jar extension for a ZIP file that contains a classes.dex entry.

    Not all versions of Dalvik can load classes from memory. You can work around this by loading the class from the file system. There’s an example in DexMaker’s generateAndLoad method:

        byte[] dex = ...
    
        /*
         * This implementation currently dumps the dex to the filesystem. It
         * jars the emitted .dex for the benefit of Gingerbread and earlier
         * devices, which can't load .dex files directly.
         *
         * TODO: load the dex from memory where supported.
         */
        File result = File.createTempFile("Generated", ".jar", dexCache);
        result.deleteOnExit();
        JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(result));
        jarOut.putNextEntry(new JarEntry(DexFormat.DEX_IN_JAR_NAME));
        jarOut.write(dex);
        jarOut.closeEntry();
        jarOut.close();
        try {
            return (ClassLoader) Class.forName("dalvik.system.DexClassLoader")
                    .getConstructor(String.class, String.class, String.class, ClassLoader.class)
                    .newInstance(result.getPath(), dexCache.getAbsolutePath(), null, parent);
        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("load() requires a Dalvik VM", e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e.getCause());
        } catch (InstantiationException e) {
            throw new AssertionError();
        } catch (NoSuchMethodException e) {
            throw new AssertionError();
        } catch (IllegalAccessException e) {
            throw new AssertionError();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I'm new to Java, so please be gentle. I have a JFrame
First off I have seen this: Remote Service as apk And it does not
I am trying to create a very basic sprite image. First off i have
First off I should say that I don't have any experience in working with
First off thanks to all the users who have made my android developing adventure
First off - apologies for the poor title, I have no idea how to
First off, I've never used GWT before. I have good experience in HTML/CSS/JS/JSP. I'm
First off, I'm aware this is a bad practice and I have answered many
First off, let me clarify the platforms we are using. We have an ASP.NET
I have two questions, actaully... First off, Why cant I do this: List<Object> object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.