0

I have class that look like this:

   public class Tests {
         public String ReturnString(){
             return "from Tests";
         }
   }

I compiled it into "Tests.class" and put it in the Root of Drive "C". I can load it perfectly fine with intellij ide in my pc like this:

   try{
        String classToLoad = "Tests";
        File file = new File("C:\\");
        URL url = file.toURI().toURL();
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls);
        Class  cls = cl.loadClass(classToLoad);
        Object instance = cls.newInstance();
        Method m = cls.getMethod("ReturnString");
        String returndString = (String) m.invoke(instance, null);
        System.out.println(returndString);
 } catch ( ClassNotFoundException | MalformedURLException | 
           NoSuchMethodException | InstantiationException | 
           InvocationTargetException | IllegalAccessException e) {
               e.printStackTrace();
 }

But when i put the same Tests.class in the root of android External Storage (or any dir) and try to load it with the same Method:

try {
            String classToLoad = "Tests";
            File file = new File(Environment.getExternalStorageDirectory().getPath());
            URL url = file.toURI().toURL();
            URL[] urls = new URL[]{url};
            ClassLoader cl = new URLClassLoader(urls);
            Class  cls = cl.loadClass(classToLoad);
            Object instance = cls.newInstance();
            Method m = cls.getMethod("ReturnString");
            String returndString = (String) m.invoke(instance, null);
            System.out.println(returndString);
        }
        catch ( ClassNotFoundException | MalformedURLException | NoSuchMethodException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }

I'll get:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.net.URLConnection java.net.URL.openConnection()' on a null object reference
                                                                            at java.net.URLClassLoader.getPermissions(URLClassLoader.java:641)
                                                                            at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoader.java:206)
                                                                            at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
                                                                            at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
                                                                            at java.net.URLClassLoader.-wrap0(URLClassLoader.java)
                                                                            at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
                                                                            at java.net.URLClassLoader$1.run(URLClassLoader.java:356)
                                                                            at java.security.AccessController.doPrivileged(AccessController.java:67)
                                                                            at java.security.AccessController.doPrivileged(AccessController.java:92)
                                                                            at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
                                                                            at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                            at java.lang.ClassLoader.loadClass(ClassLoader.java:312)

i know the problem its not with the path because if i delete the Tests.class from the root of android External Storage ill get: "java.lang.ClassNotFoundException: Tests"

Thank!

Ron Partuk
  • 39
  • 1
  • 6
  • That `.class` file is probably useless on Android, since Android uses DEX files. You [may be able to load a DEX file dynamically though](https://stackoverflow.com/questions/27349097/load-dex-file-dynamically-on-android-5-0) (I haven't tried it myself). – Michael Aug 04 '18 at 12:44
  • Check that https://android-developers.googleblog.com/2011/07/custom-class-loading-in-dalvik.html – Veselin Davidov Aug 04 '18 at 12:50
  • Thanks @VeselinDavidov , It helps a lot! – Ron Partuk Aug 04 '18 at 19:27

0 Answers0