2

I am trying to send an object from a server app to an android client. All of the classes are in the same package and in the src folder in eclipse, including the one which is causing the ClassNotFoundException: CardGame. I am new to android and fairly new to java, and have been stuck on this for a couple days, so I'm wondering if someone would be able to help me figure out what the problem is...

Here is the code that is causing the issue...

CardGame game = (CardGame) in.readObject();

here is the stack trace...

04-21 19:21:35.608: W/System.err(482): java.lang.ClassNotFoundException: CardGame
04-21 19:21:35.618: W/System.err(482):  at java.lang.Class.classForName(Native Method)
04-21 19:21:35.618: W/System.err(482):  at java.lang.Class.forName(Class.java:235)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2590)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1846)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
04-21 19:21:35.628: W/System.err(482):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
04-21 19:21:35.628: W/System.err(482):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
04-21 19:21:35.628: W/System.err(482):  at com.client.activity.play.init(play.java:107)
04-21 19:21:35.628: W/System.err(482):  at com.client.activity.play.onCreate(play.java:54)
04-21 19:21:35.628: W/System.err(482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-21 19:21:35.638: W/System.err(482):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 19:21:35.638: W/System.err(482):  at android.os.Looper.loop(Looper.java:123)
04-21 19:21:35.638: W/System.err(482):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 19:21:35.638: W/System.err(482):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 19:21:35.638: W/System.err(482):  at java.lang.reflect.Method.invoke(Method.java:521)
04-21 19:21:35.638: W/System.err(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 19:21:35.638: W/System.err(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 19:21:35.648: W/System.err(482):  at dalvik.system.NativeStart.main(Native Method)
04-21 19:21:35.648: W/System.err(482): Caused by: java.lang.NoClassDefFoundError: CardGame
04-21 19:21:35.648: W/System.err(482):  ... 24 more
04-21 19:21:35.658: W/System.err(482): Caused by: java.lang.ClassNotFoundException: CardGame in loader dalvik.system.PathClassLoader[/data/app/com.client.activity-1.apk]
04-21 19:21:35.658: W/System.err(482):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-21 19:21:35.668: W/System.err(482):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-21 19:21:35.668: W/System.err(482):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-21 19:21:35.668: W/System.err(482):  ... 24 more

Thank you!

3 Answers 3

4

Turns out that the class of the object that is being sent through the ObjectOutputStream must have the same package name on both the server, and on the client device. This seems to have fixed my problem. Thanks to everyone for the help.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Had a similar problem, and fixing package names solved it!
This worked for me sending between two Android devices from v4.3 to v4.1.2
Nice one Matt. Worked for me too
1

It's worth noting that serialization between different VM's (e.g. Android's Delvik and the Sun JVM) may not be compatible.

I'd recommend if you do server / Android communications then you should rely on something like XML or JSON (I prefer the latter and there are some Android classes that make it easier)

see: http://developer.android.com/reference/org/json/JSONObject.html

2 Comments

it worked on my project google woild be stupid if he would ensure compatibility
Worked for me too.. Did not have any issue
0

It's hard to say much more without being able to see your Eclipse setup, but that error indicates that your CardGame class is not ending up getting package into the APK that's getting built.

Is that class in the Android project or is it in the Server project? If it's in the server project, make sure that project is being referenced by the Android project and that your CardGame class is getting exported properly and is visible in that project.

If you look at the source code in Eclipse with the line:

CardGame game = (CardGame) in.readObject();

Is it finding CardGame OK? If not, then your classpath setup is bad. If yes, then the Android build is not pulling in all the necessary resources when building the APK.

1 Comment

Thank you. If all I do is declare CardGame game; then no error is reported. How could I get android to build the APK properly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.