ClassNotFoundException when including a library jar
Solution 1
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:
<javac srcdir="." destdir="bin/classes" source="1.6" target="1.6">
...
</java>
Or you can use these steps in 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.
Solution 2
You're getting a NoClassDefFoundError
because your jar file is not available at runtime.
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:
Solution 3
(java build path / order and export) move your lib on top and check in
Solution 4
For IntelliJ this setup finally made the error go away for me:
Nitzan Tomer
fugazi.io fugazi.io is a web based terminal application for executing local and remote commands which uses user-defined commands syntax. I've been working on it for a while now as a side project, and we're looking for help and feedback, so feel free to contact us (terminal.fugazi.io AT gmail.com). Check out the github repo. Also: software engineer web developer geek aikidoka kendoka yogi juggler (retired) skateboarder (occasional) snowboarder (24/7) smart-ass
Updated on June 23, 2022Comments
-
Nitzan Tomer almost 2 years
In my android project I added a library jar into the
libs
folder.
This jar contains three different packages:- 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)
The jar is compiled/archived using ant.
There are no errors in eclipse, but when I try to run my android app on a device I get this:
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.access$600(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.
Using apktool I 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.My environment:
Eclipse 4.2.1
ADT 21.0.1
Java 7 (oracle)Devices I tested on:
Samsung galaxy S2
LG nexus 4Any ideas? Thanks.
-
Nitzan Tomer over 11 yearsI don't have the jar in the
Order and Export
tab, only the src and gen folders and the Android Dependencies and Android 4.2. But as I wrote in my question, some content of my jar is being added to the apk, but not all of it. So it's not true that my jar is not available at runtime. -
Nitzan Tomer over 11 yearsI don't have the jar in the Order and Export tab, only the src and gen folders and the Android Dependencies and Android 4.2.
-
Anand over 11 years(java build path / libraries) click on add jar and add you gson jar file from libs folder
-
Nitzan Tomer over 11 yearsFrom what I've read, in the newer versions of ADT you don't need to do that, in fact you should not do that. It's supposed to be enough to add the needed libraries to the
libs
directory of the project. -
Ahmad over 11 yearsI haven't read about that. AFAIK you still need to add the jar to your build path.