Reputation: 1508
I have created a small application that connects to a server that can handle more than one connections. In my java project I have the following classes:
Client.java
Client who connects to the server (Application 1)Server.java
Waits for connections and start a ServerThread (see below) (Application 2)ServerThread.java
Each connection has its own thread that runs in this classCustomObject.java
An object that contains a few different variables that I send back and forth from the server.I send it like this:
out.writeObject(new CustomObject()); // out = objectOutputStream
And then I receive at the server application like this:
co = (CustomObject) in.readObject(); // in = objectInputStream
And this works great when I run it within my project (first start the server application and then two instances of the client application). However if the Server and ServerThread classes aren't in the same eclipse project as the Client and CustomObject classes it gives me an error for the last line of code I posted where I cast in.readObject();
. "ClassNotFoundException" (even though the server project has an exact copy of CustomObject).
How can I fix this? I want to be able to export and run the server as one program and then the client as one when I export them from eclipse. Let me know if something is unclear.
Upvotes: 1
Views: 1195
Reputation: 80176
You should have the class definition available on the JVM where client is running for the readObject() to be able to successfully de-serialize it. So you should have two jar files as below
Server Jar
Client Jar
You can use RMI as a higher level abstraction if you are writing a big application.
Upvotes: 0
Reputation: 40335
The easiest solution is to put the server and client in the same Eclipse project:
Both Server and Client can have a main() and be in the same project.
Create two separate run configurations in eclipse (one for Server, one for Client) -- note that run configurations are created automatically if they don't exist when you press the "play" button in eclipse (just make sure the file that has the main() method is open in the editor when you press play), so you probably won't need to do this manually.
Then, in eclipse, when you export the program, export it as a Runnable JAR (if "Runnable JAR" isn't available in the export quick menu just select "Other..." and search for it). You can select the run configuration. So export one for client (maybe name it client.jar) and one for the server (maybe name it server.jar).
You can have many applications in one project, as long as you select the appropriate run configuration when you export a Runnable JAR.
Another solution, which is more common for larger projects when perhaps every application shouldn't be in the same project file (e.g. you don't want to distribute the code for one application with another, or maybe some are not built with Eclipse, etc.), is to put the common stuff in its own project. Then, you can either a) add that project to the build configuration for the other projects you are using it in, or b) export it as a JAR and use that JAR in your other projects.
Upvotes: 1