eclipse 包含库 jar 时出现 ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14625540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassNotFoundException when including a library jar
提问by Nitzan Tomer
In my android project I added a library jar into the libs
folder.
This jar contains three different packages:
在我的 android 项目中,我在libs
文件夹中添加了一个库 jar 。
这个 jar 包含三个不同的包:
- google GSON (com.google.gson)
- jboss netty (org.jboss.netty)
- A client application I developed myself which uses these two other libs. (com.example.core)
- 谷歌 GSON (com.google.gson)
- jboss netty (org.jboss.netty)
- 我自己开发的客户端应用程序使用这两个其他库。(com.example.core)
The jar is compiled/archived using ant.
jar 是使用 ant 编译/存档的。
There are no errors in eclipse, but when I try to run my android app on a device I get this:
eclipse 中没有错误,但是当我尝试在设备上运行我的 android 应用程序时,我得到了这个:
E/AndroidRuntime(23807): FATAL EXCEPTION: main
E/AndroidRuntime(23807): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
E/AndroidRuntime(23807): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(23807): at android.app.ActivityThread.access0(ActivityThread.java:141)
E/AndroidRuntime(23807): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(23807): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(23807): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(23807): at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(23807): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(23807): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(23807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(23807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(23807): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(23807): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.project.MainActivity" on path: /data/app/com.example.project-1.apk
E/AndroidRuntime(23807): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
E/AndroidRuntime(23807): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime(23807): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime(23807): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
E/AndroidRuntime(23807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
E/AndroidRuntime(23807): ... 11 more
And it's definitely my jar that is causing this, since if I remove references to it's classes from within the MainActivity then the problem is solved and the app runs on the device.
这绝对是我的 jar 造成的,因为如果我从 MainActivity 中删除对它的类的引用,那么问题就解决了,应用程序在设备上运行。
Using apktoolI checked to see what goes into the apk, and I found that everything is being added to the smali
directory, that is everything except for my package (com.example.core).
So all of the android app classes (com.example.project), the gson (com.google.gson) and netty (org.jboss.netty) from my jar are there but not com.example.core.
使用apktool我检查了 apk 中的内容,我发现所有东西都被添加到smali
目录中,这是除了我的包(com.example.core)之外的所有东西。
因此,我的 jar 中的所有 android 应用程序类 (com.example.project)、gson (com.google.gson) 和 netty (org.jboss.netty) 都在那里,但没有 com.example.core。
My environment:
Eclipse 4.2.1
ADT 21.0.1
Java 7 (oracle)
我的环境:
Eclipse 4.2.1
ADT 21.0.1
Java 7 (oracle)
Devices I tested on:
Samsung galaxy S2
LG nexus 4
我测试的设备:
三星galaxy S2
LG nexus 4
Any ideas? Thanks.
有任何想法吗?谢谢。
回答by Ahmad
You're getting a NoClassDefFoundError
because your jar file is not available at runtime.
你得到一个,NoClassDefFoundError
因为你的 jar 文件在运行时不可用。
In order for it to be available at runtime you'll have to check the checkboxes on your jar file in your java build path like this:
为了使其在运行时可用,您必须在您的 java 构建路径中检查 jar 文件上的复选框,如下所示:
回答by Nitzan Tomer
Ok, I finally managed to find the problem, it took me a while.
好的,我终于找到了问题所在,花了我一段时间。
I am using java 7 on my machine and the ant builder uses this default version as well, unfortunately though, android does not like java 7, and so this fixed the problem for me:
我在我的机器上使用 java 7 并且 ant builder 也使用这个默认版本,但不幸的是,android 不喜欢 java 7,所以这为我解决了这个问题:
<javac srcdir="." destdir="bin/classes" source="1.6" target="1.6">
...
</java>
Or you can use these steps in Eclipse:
或者您可以在 Eclipse 中使用这些步骤:
Eclipse menu: Window/Preferences/Java/Compiler
Compiler Compliance level: 1.6
tick "use default"
Hope that this will save some time for others who are facing this problem.
希望这将为面临此问题的其他人节省一些时间。
回答by Anand
(java build path / order and export) move your lib on top and check in
(java 构建路径/顺序和导出)将您的库移到顶部并签入
回答by IHeartAndroid
For IntelliJ this setup finally made the error go away for me:
对于 IntelliJ,此设置最终使我的错误消失了: