Question

I need to know how to load a class which doesn't have a package, using a different classloader (test.jar contains the classes.dex file):

File f = new File("/sdcard/test.jar");
final File optimizedDexOutputPath = context.getDir("outdex", 0);
DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(), optimizedDexOutputPath.getAbsolutePath(), null, context.getClassLoader());

If "Test" class has a package:

package com.test;

public class Test {
    public static void test() {
        // do something
    }
}

This works:

Class<?> myClass = classLoader.loadClass("com.test.Test");

If "Test" class doesn't have a package:

public class Test {
    public static void test() {
        // do something
    }
}

This doesn't work:

Class<?> myClass = classLoader.loadClass("Test");

Is it possible to load a class without a package?

Was it helpful?

Solution

There is no problem to load class without package name. Check my example (Class "Test" must has no package):

public class Test {

    public static void main(String[] args)  {
        try {
            Class<?> myClass = Test.class.getClassLoader().loadClass("Test");
            System.out.println(myClass);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

You will got console output without exception:

class Test

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top