0

I'm trying to cast an object passed by my client application to an object of identical class in my server application.

The error I'm currently receiving in my server application is:

mei 02, 2012 11:44:43 PM server.UserThread process
SEVERE: null
java.lang.ClassNotFoundException: client.User

The object is being received by the server via Socket -> ObjectInputStream.

So I was wondering if you guys could help me cast the client.User class to my Server.User class. The only thing that works is placing the packages inside 1 project and then defining the exact location of the class.

Code can always be supplied.

8
  • 1
    "ClassNotFoundException" suggests that your server doesn't have access to the same class file being used in the client. Are you doing something funny with classloaders? Commented May 2, 2012 at 22:18
  • 1
    Did you create 2 identical classes or are you using the same class? You should be using the later. Commented May 2, 2012 at 22:20
  • Showing some code might help. Commented May 2, 2012 at 22:21
  • I'm not using classloaders atm. Commented May 2, 2012 at 22:23
  • 1
    That's the exact opposite of what I just said. Commented May 2, 2012 at 22:52

2 Answers 2

2

cast the client.User class to my Server.User class

You can't. They aren't the same class. They are in different packages. They are different. If you want to share a class between server and client, you have to share a single .class file. That might imply creating a third package for shared stuff.

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

2 Comments

Thank you after a bit of trial and error I found the perfect solution. This helped a lot.
You found? I provided.
0

The classes need to be the same, as some users suggest in reply to your question. It doesnt seem like a good choice to use the default serialization procces to flatten and inflate your objects. You can define your own protocol if you use the Externalizable interface, instead of Serializable, you can use writeExternal and readExternal to customize the serialization procces.

If you are trying to send object from server to client, I really discourage the use of the object streams. May I suggest Protobuff to do this?

Protobuff:

Protocol Buffers are a way of encoding structured data in an efficient yet extensible format. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats.

Comments

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.