3

I have 2 projects: dummy and dummy-android. dummy-android uses Game class from dummy. I included dummy in Build Path of dummy-android but when run on emulator. It generates:

E/AndroidRuntime(  691): java.lang.NoClassDefFoundError: com.dummy.Game
E/AndroidRuntime(  691):    at com.dummy.activity.AndroidGame.onCreate(AndroidGame.java:13)
E/AndroidRuntime(  691):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  691):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  691):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  691):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  691):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  691):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  691):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  691):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  691):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  691):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  691):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  691):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  691):    at dalvik.system.NativeStart.main(Native Method)

enter image description here

I tried to remove dummy from dummy-android's Build Path, clean up, re-include it, clean up but it still doesn't work.

My project doesn't show any compile errors! This is a bug of Eclipse! How to fix it? Thank you.

Update: Tried to restart Eclipse, cleanup two projects many times, re-run, nothing changes.

Update: @Martin Brakash. I unchecked project specific settings in both projects. It generates a new exception:

05-15 04:48:14.575: E/AndroidRuntime(971): FATAL EXCEPTION: main
05-15 04:48:14.575: E/AndroidRuntime(971): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dummy.activity/com.dummy.activity.AndroidGame}: java.lang.ClassNotFoundException: com.dummy.activity.AndroidGame in loader dalvik.system.PathClassLoader[/data/app/com.dummy.activity-1.apk]
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.os.Looper.loop(Looper.java:123)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-15 04:48:14.575: E/AndroidRuntime(971):  at java.lang.reflect.Method.invokeNative(Native Method)
05-15 04:48:14.575: E/AndroidRuntime(971):  at java.lang.reflect.Method.invoke(Method.java:521)
05-15 04:48:14.575: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-15 04:48:14.575: E/AndroidRuntime(971):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-15 04:48:14.575: E/AndroidRuntime(971):  at dalvik.system.NativeStart.main(Native Method)
05-15 04:48:14.575: E/AndroidRuntime(971): Caused by: java.lang.ClassNotFoundException: com.dummy.activity.AndroidGame in loader dalvik.system.PathClassLoader[/data/app/com.dummy.activity-1.apk]
05-15 04:48:14.575: E/AndroidRuntime(971):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-15 04:48:14.575: E/AndroidRuntime(971):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-15 04:48:14.575: E/AndroidRuntime(971):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-15 04:48:14.575: E/AndroidRuntime(971):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-15 04:48:14.575: E/AndroidRuntime(971):  ... 11 more
emeraldhieu
  • 9,380
  • 19
  • 81
  • 139
  • I've had the same problem. In a java application I added GSON as an external library and it worked just fine. But in an android app it would just throw a noclassdeffound. – user1378730 May 15 '12 at 04:06
  • I've never seen this error but it could be because your packages are named identically. Does this still happen if project dummy has a unique package name? – thagorn May 15 '12 at 04:07
  • As I said no compile errors showed. – emeraldhieu May 15 '12 at 04:09
  • The android plugin to eclipse isn't perfect, it could be the compiler that is missing something rather than the JVM. – thagorn May 15 '12 at 04:10
  • 1
    Try exporting dummy project to build class path: `Build Path -> Configure Build Path -> Order and Export -> tick dummy project` – yorkw May 15 '12 at 04:15
  • @yorkw, in Order and Export, "dummy/src" is checked by default. – emeraldhieu May 15 '12 at 04:20
  • 2
    Check if both the projects have same JDK compliance level in case you have enabled project specific settings (right click ->properties->Java Compiler. – Martin Prakash May 15 '12 at 04:43
  • @Emerald214, are you sure your dummy-android project compiled without errors after you disabled project specific settings? – Martin Prakash May 15 '12 at 05:09
  • I'm sure bro... This is a weird error I've ever met. If there is compiled error, how could I run it and get those exceptions? :-S – emeraldhieu May 15 '12 at 09:15

4 Answers4

2

The error message is telling you that you are not including the jar file in your project. Create a libs folder in the root of your android project and place the jar files you need into the libs folder.

user1378730
  • 930
  • 9
  • 18
itechDroid
  • 1,031
  • 1
  • 11
  • 17
  • 1
    I include a class (Game) from other project, not jar file. – emeraldhieu May 15 '12 at 04:23
  • u can also add jar file by other method like open project properties-> java build path-> libraries-> add external jar now click on this add external jar file and add jar file which u want .. – itechDroid May 15 '12 at 04:33
  • Ash is correct, my method must be for something different. I did what he said and created a libs in the root of my android project and added the GSON.jar I was having trouble with. – user1378730 May 15 '12 at 04:45
  • This problem isn't related to including jar file. I'm using a CLASS from other project. Game class and AndroidGame class are in source folder, not jar file. – emeraldhieu May 15 '12 at 09:22
1

I had exactly the same problem and I can't understand how Eclipse can't take the reference to the other project by including it. Nevertheless this is how I solved it:

1) Build Path -> Configure Build Path -> Tab Source -> Link Source

Browse for Link Folder location: /pathToMyProject/src
Folder name: src/newFolder

2) Tab Libraries -> Add Class Folder

/pathToMyProjectr/bin

That made it work for me! I Hope it helps.

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Martín C
  • 1,027
  • 1
  • 9
  • 13
0

Right, sorry for leading you on a goose chase. I think the users-library must be for optional google stuff like their maps api.

http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject

This sounds like the one you need.

Right click on the project you want to import then Properties > Android > Check Is Library.

Then on the other project get to the same menu but click add instead.

You can now import it like any other class also the link has pictures if you a bit lost.

user1378730
  • 930
  • 9
  • 18
0

In my case the problem was that the java compiler was set to 1.7 on the java project and to the 1.6 in the android project.

I changed so they will be the same and the problem was solved. (lost a lot of hours though).

Oh, and I used the "export to jar" -> "place it in the libs folder" -> "add to java build path" method.

Cheers