Avatar of ajaycv
ajaycv
 asked on

ClassNotFoundException in Client when trying to access stub on a different server (Java RMI)

Hello Experts,

I have my server class running on another server. My client is trying to access the remote object and it is throwing the follwoing error:

error unmarshalling return; nested exception is:
      java.lang.ClassNotFoundException: JoptServer_Stub
java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
      java.lang.ClassNotFoundException: JoptServer_Stub

The stub named JoptServer_Stub is available on the server. I am able to successfully bind the remote object to the RMI Engine. I am also attaching the files for your reference.

Please let me know the solution to this problem.

P.S: My client and server are not on the same machine.
Client.java
JoptRemoteCalls.java
JoptServer.java
JavaJava EE

Avatar of undefined
Last Comment
ajaycv

8/22/2022 - Mon
Gurvinder Pal Singh

Are you getting this exception on the server's console or client's console?
anilallewar

The stub for the remote RMI server generated through rmic tool has to be available on the client classpath for the RMI invocation to work. The way the RMI communication passes is

Client<--->stub<--->[NETWORK]<--->skeleton<--->Server

So either create a jar of the stub classes generated through rmic or place the classes directly in the client classpath.
ASKER CERTIFIED SOLUTION
anilallewar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Gurvinder Pal Singh

Your help has saved me hundreds of hours of internet surfing.
fblack61
ajaycv

ASKER
Thanks for providing the solutions. I generated the stub on the client and it worked. But I will try the solution provided by anilallewar and will post the results here.

One question to anilallewar - Do you mean to say that the server class should not have a main method?
anilallewar

Nope..the server class needs to have the main method for Java to call.However since your main method instantiates the registry and completes, it will take the registry down with it when it completes.

Now when the client makes RMI call, the registry is down and the stub can't be retrieved. It mightbe the case that the registry is still running if it was launched as a daemon; however the server stub registered with the server is removed since the server class itself is finished executing.

Hence you need to keep the server class running infinitely by adding the code I provided.
ajaycv

ASKER
I am getting a Class Cast Exception from the stub. Can you please help?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ajaycv

ASKER
Fixed it. The solution is that the server stub should be in the same package as on the client or vice versa.
anilallewar

Glad if I could be of help in fixing your prob!!
ajaycv

ASKER
The tips were helpful
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes