Java 在运行时加载已编译类
我有这样的课程:Java 在运行时加载已编译类,java,android,classloader,urlclassloader,Java,Android,Classloader,Urlclassloader,我有这样的课程: public class Tests { public String ReturnString(){ return "from Tests"; } } try{ String classToLoad = "Tests"; File file = new File("C:\\"); URL url = file.toURI().toURL();
public class Tests {
public String ReturnString(){
return "from Tests";
}
}
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”,并将其放在驱动器“C”的根目录中。
我可以在pc中使用intellij ide将其完全加载,如下所示:
public class Tests {
public String ReturnString(){
return "from Tests";
}
}
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外部存储的根目录中删除Tests.class,我会得到:java.lang.ClassNotFoundException:Tests
谢谢! 这个
.class
文件在Android上可能没有用处,因为Android使用DEX文件。你(我自己还没试过)。检查一下谢谢@VeselinDavidov,这很有帮助!