在运行时加载编译的类

时间:2018-08-04 12:38:32

标签: java android classloader urlclassloader

我有一个看起来像这样的课:

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

我将其编译为“ Tests.class”,并将其放入驱动器“ C”的根目录中。 我可以像这样在我的PC中使用intellij ide完美加载它:

   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();
 }

但是当我将相同的Tests.class放入android外部存储(或任何目录)的根目录并尝试使用相同的方法加载它时:

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();
        }

我会得到:

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)

我知道问题不在于路径,因为如果我从android external storage的根目录中删除Tests.class会得到:“ java.lang.ClassNotFoundException:Tests

谢谢!

0 个答案:

没有答案