所以我正在尝试加载我从我的应用程序中编写的类,但我收到 ClassNotFound 异常。
班级:
public class myclass{
public String doSomething()
{
return "Hello from myclass";
}
}
然后我使用以下命令创建一个 jar 文件:
javac myclass.java
jar cvf myclass.jar myclass.class
我的下一步是为我的 jar 文件创建一个 classes.dex,并将其添加到 jar 中:
dx --dex --output=classes.dex myclass.jar
aapt add myclass.jar classes.dex
到目前为止,一切正常,没有错误。
这一步结束时,我的jar文件如下:
myclass.jar
-> classes.dex
-> myclass.class
-> META-INF
-> MANIFEST.MF
我的清单文件包含以下几行:
Manifest-Version: 1.0
Created-By: 1.7.0_79 (Oracle Corporation)
现在在我的应用程序中,我正在这样做:
private void activate()
{
try {
URL url;
final String libPath = Environment.getExternalStorageDirectory() + "/myclass.jar";
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader(libPath, tmpDir.getAbsolutePath(), null, this.getClass().getClassLoader());
final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("myclass");
final Object myInstance = classToLoad.newInstance();
final Method doSomething = classToLoad.getMethod("doSomething");
String result = (String)doSomething.invoke(myInstance);
Toast.makeText(MainActivity.this, String.valueOf(result), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
笔记:
- 我将 myclass.jar 文件复制到
Environment.getExternalStorageDirectory()
目录,我确认应用程序正在正确读取文件。
- 我已经在清单文件中设置了所需的权限(读写到外部存储)
给我错误的行是:
final Class<Object> classToLoad = (Class<Object>) classloader.loadClass("myclass");
错误:
Didn't find class "myclass" on path: DexPathList[[zip file "/storage/emulated/0/myclass.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
这是被抑制的异常:
java.io.IOException: No original dex files found for dex location /storage/emulated/0/myclass.jar
java.lang.ClassNotFoundException: Didn't find class "myclass" on path: DexPathList[[dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-support-annotations-23.3.0_d42c1a3ea55c8ce3f82fc6c8adde2e2271b97d01-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-internal_impl-23.3.0_75eb76bdd9c015b48a4280667bfd60eddc59734a-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.3.0_0a5b88d45587f58c7ea931638fd9c9d6c4641bc7-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-support-v4-23.3.0_a0a32aaad2874bf2c15ffa67543dfe5af5b91a95-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.3.0_eae70f9c3956161dfb324670cb40cb42e6e1b16a-classes.dex", dex file "/data/data/hayzohar.testingclassloaders/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.3.0_d4513d6bda757fa567bc4d7ea3e4bb7cbd09c077-classes.dex"],nativeLibraryDirectories=[/data/app/hayzohar.testingclassloaders-2/lib/x86, /vendor/lib, /system/lib]]
我究竟做错了什么?