First, I want to mention that I read many stackoverflow posts about NoClassDefFoundError, and I also read about it in many other blogs and websites, but the solutions that people offered didn't fix it.
I am running Eclipse 64-bit with the ADT plugin version v21.0.1-543035 on Ubuntu 12.10 64-bit. Everything is 64-bit, Ubuntu, Eclipse and the JRE and JDK that I use (jdk1.6.0_38).
I wrote a very small Android App that needs a class from the JDK to run.
I isolated the problem a little, and recreated it by creating a new "Android Application Project" with ONLY ONE LINE OF MY CODE (in the main class in the onCreate method). This line:
BufferedImage buff = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
Eclipse automatically adds the necessary import:
import java.awt.image.BufferedImage;
but asks that I add the jars/JRE for this class.
I added the jdk1.6.0_38 to Eclipse in "Installed JREs" (like they instruct on the Eclipse help pages).
In the project's "Java Build Path" I added it through "Add library" -> "JRE system library" -> "Workspace default JRE". It automatically added the JDK to the project's build path.
During compile time, I get no errors. Only when running the application in the android emulator (any AVD) I get the following error:
E/dalvikvm(828): Could not find class 'java.awt.image.BufferedImage', referenced from method com.example.usejdk.MainActivity.onCreate
W/dalvikvm(828): VFY: unable to resolve new-instance 467 (Ljava/awt/image/BufferedImage;) in Lcom/example/usejdk/MainActivity;
D/dalvikvm(828): VFY: replacing opcode 0x22 at 0x0009
D/dalvikvm(828): DexOpt: unable to opt direct call 0x0cdc at 0x0c in Lcom/example/usejdk/MainActivity;.onCreate
D/AndroidRuntime(828): Shutting down VM
W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
E/AndroidRuntime(828): FATAL EXCEPTION: main
E/AndroidRuntime(828): java.lang.NoClassDefFoundError: java.awt.image.BufferedImage
E/AndroidRuntime(828): at com.example.usejdk.MainActivity.onCreate(MainActivity.java:16)
E/AndroidRuntime(828): at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(828): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(828): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
Here are the things I tried that didn't work:
- adding to "eclipse.ini" the location of the jdk ("-vm /usr/lib/jvm/jdk1.6.0_38/bin" in 2 separate lines).
- I added the "Default VM Arguments" of the path "-Djava.library.path=/usr/lib/jvm/jdk1.6.0_38/jre/lib".
- Using 32-bit JREs and JDKs and JRE/JDK7 - didn't work (of course).
- I tried adding the jdk jar files manually to the "libs" folder and mark "add to buildpath" - didn't work.
- I uninstalled and installed Eclipse again with the ADT and Android SDK...
Nothing fixes this annoying error...
Please, also notice these:
- I made sure my .classpath file contains the default JRE entry (like they say here)
- I tried adding other jar files to the "libs" folder + "add to path", and using them - it worked fine.
- I tried running the same code (the one line) using the "import java.awt.image.BufferedImage;" in a regular java project (not an android project) and made the project "use Default JRE" - and it worked fine! Why does it work on a REGULAR Java project and not in an Android project?
I guess it means that my Eclipse installation does have the ability to use the jdk classes, but not in android applications.
So what do I have to do to make this single line of code run in an android application?
Any help would be VERY-VERY appreciated. Thanx in advance.