This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Distributed Java and the fly likes Servlet as an rmi client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Servlet as an rmi client" Watch "Servlet as an rmi client" New topic

Servlet as an rmi client

Diya Shankar

Joined: Jan 05, 2007
Posts: 5
I am trying to set up a servlet as an rmi client. My rmi server runs on a remote machine. I have the rmi server up and running. Now I am trying to write a servler client to talk to this server which exists on a remote machine. I am developing servlet in my desktop using eclipse and tomcat.

When I try to do a lookup, I get AccessControlException.

How do I solve it ?

Servlet in localhost and RMI server in remote machine.
Please help
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

It sounds like the user, under which your container is running doesn't have sufficient permissions to access the the network resource in question.

Personally, I wouldn't do this directly in a servlet.
I would make the RMI client a plain old Java object that can be instanciated by any interface (command line, servlet, etc...) to have it's methods called.
This will make debugging things like this much easier.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Diya Shankar

Joined: Jan 05, 2007
Posts: 5
Thanks for your response Ben. But based on our requirement, I have to have a servlet for accessing RMI server. I managed to get thru that problem by changing java.policy file in my local machine.

Now, I get another exception, "java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: TestServer_Stub

I believe for this I have to set up a codebase.
My server runs in an UNIX machine. I read in an article that place the rmi classes[or jar file with rmi stub etc classes] in a directory which can be accessed via http.

and then use -Djava.rmi.server.codebase property from client side.
Has anyone tried this ?

Again client is in Eclipse/tomcat

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

But based on our requirement, I have to have a servlet for accessing RMI server.

Strange that someone would require such a poor design.

In any case....
At this point, it sounds like your issue has more to do with RMI than with anything particular to servlets.

I'm going to move this to Distributed Java
[ January 05, 2007: Message edited by: Ben Souther ]
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Yes, its a CLASSPATH problem.

Codebase does work, but I've never used it with servlets.

Ed's latest article: A Java Parallel Calamity
Diya Shankar

Joined: Jan 05, 2007
Posts: 5
have you accessed a rmi server in a remote machine from a local application ?

If yes, how did you set the code base then ?
where did you out the classes and which protocol did you use ?
Chad Naber

Joined: Mar 25, 2005
Posts: 8
Hi, that second error message looks to be a classpath problem.

Make sure the library containing the stubs is included in the classpath when starting up RMIRegistry. I would recommend just doing a simple set and export of the CLASSPATH before starting up RMIRegistry.
Diya Shankar

Joined: Jan 05, 2007
Posts: 5

Hi This is what I do for starting up rmi registry.

and then I start rmiregistry like this:
nohup rmiregistry -J-mx128m -J-ms16m -J-oss1m -J-ss1m \
-J-Djava.rmi.dgc.leaseValue=123400000 \
-J-Djava.rmi.server.logCalls=true 3444> \
RmiRegistry3444.log 2>&1 &
sleep 10

I dont specify any codebase when I start rmiregistry. Again this is on a remote machine.

Any suggestions ?
Diya Shankar

Joined: Jan 05, 2007
Posts: 5
Can any experts help me please ?

I have displayed the way my registry gets started. This rmi server is in a remote machine.

I have my servlet which runs in my tomcat.

I tried putting the server's stub files etc as jar file in Unix server's public_html directory and setting-Djava.rmi.server.codebase= in my tomcat's configuration.

Still I get lots of exceptions, Unmarshalling exception etc.

Please help
Chandan Phukan

Joined: Jan 10, 2007
Posts: 3
A few things about it..

I am having the same problem but my servlet runs well if I don't run the rmiregistry programatically.

1. run the rmiregistry manually.
In the server code,

bind the object as :
YourServerImplClass implObj=new YourServerImplClass ();

In the servlet get the object as :
YourServerInterface infObj=(YourServerInterface)Naming.lookup("rmi://IP address wheree your rmi server runs/implObj");

Now with the help of the infObj you can access any method.

Note : Copy the servlet in webapps/ROOT/WEB-INF/classes folder.
You have to copy the stub class as well as your interface class in the classes folder.

That's all.I think it should work.As its working fine with me..

best wishes....
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Servlet as an rmi client
It's not a secret anymore!