Eclipse: java.lang.NoClassDefFoundError for JDK classes in an Android project

Firstly, I want to mention that I read many stackoverflow posts about NoClassDefFoundError , and I also read about it in many other blogs and sites, but the solutions that people suggested didn't fix it.

I am running Eclipse 64-bit with the plugin version of ADT version 21.1.1-543035 on Ubuntu 12.10 64-bit . These are all 64-bit, Ubuntu, Eclipse and JRE and JDK that I use (jdk1.6.0_38).

I wrote a very small Android application that needs a class from the JDK to run.

I fixed the problem a bit 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 to add jars / JRE for this class.

I added jdk1.6.0_38 to Eclipse in the "Installed JREs" ( as they instruct on the Eclipse help pages ).

In the “Java Build Path” project, I added it through “Add Library” → “System JRE Library” → “Default JRE Workspace”. It automatically adds JDK to the project creation path.

During compilation, I get no errors. Only when I run 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 is what I tried to not work :

  • adding the location jdk in " eclipse.ini " ("-vm / usr / lib / jvm / jdk 1.6.0_38 / bin" in two separate lines).
  • I added "Default VM Arguments" paths -Djava.library.path = / usr / lib / jvm / jdk1.6.0_38 / jre / lib ".
  • Using 32-bit JREs and JDKs and JRE / JDK7s did not work (of course).
  • I tried to add jdk jar files manually to the "libs" folder and flagging "add to buildpath" - did not work.
  • I uninstalled and installed Eclipse again using the ADT and Android SDK ...

Nothing corrects this annoying error ...

Also note:

  • I made sure my .classpath file contains the default JRE entry ( as they say here )
  • I tried to add other jar files to the "libs" + "add to path" folder and use them - it worked fine.
  • I tried to run the same code (single line) using "import java.awt.image.BufferedImage;" in a regular java project ( not an android project ) and made the project “Use standard JRE” - and it worked fine! Why is this working on a REGULAR Java project and not on an Android project?

I assume this means that my Eclipse installation has the ability to use jdk classes, but not in Android apps .

So what I need to do to make this single line of code in an Android application. ?

Any help would be VERY VERY appreciated. Thanx in advance.

+4
source share
1 answer

So what do I need to do to make this one line of code run in an Android application?

The short answer is: nothing, because you can't do anything about it. The entire java.awt.* Infrastructure is not part of the Android SDK, including BufferedImage and therefore is not supported by the emulator or physical devices. Android has its own implementation for loading and rendering graphics.

Not knowing what you need BufferedImage , it's hard to say which alternative you should consider. Most likely, you are trying to do some kind of image manipulation, which involves accessing individual pixels in the image? If so, check out Bitmap and BitmapFactory .

It all boils down to the Java SDK != Android SDK . In any case, also consider searching here on SO; You are not the first to make this mistake.

+7
source

All Articles