RMI Gotchas: Darn it! Not again…


It shouldn‘t be this wicked. Ok, I admit it has been a while since I implemented
a Java RMI based
client/server. But, it shouldn‘t be this wicked. So, I was trying to get some
RMI client/server code running on my machine. And I kept getting was this friggin
exception:

Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ServerException:
RemoteException occurred in server thread;
nested exception is: java.rmi.UnmarshalException: error unmarshalling arguments;
nested exception is: java.lang.ClassNotFoundException: com.sun.salsa.PatternModelImpl_Stub

The RMI docs said that anytime you get a ClassNotFoundException, it
is most likely caused by an improperly set codebase[J] So I checked
the value of my java.rmi.server.CodeBase property. It was set as follows:

java.rmi.server.CodeBase=file://dev/salsa/classes 

I checked to make sure that the directory was there and that all the stubs
and other classes were in there. They were. So, I ran the RMI server program
again. But, again I got the same exception. What the heck is going on? And then
it hit me. Isn‘t that supposed to be codebase and not CodeBase?

So I change "CodeBase" to "codebase" and reset the property
as follows:

java.rmi.server.codebase=file://dev/salsa/classes 

And give the program another try. This time, I got a different exception.

Exception in thread "main" java.security.AccessControlException:
access denied (java.io.FilePermission //dev/salsa/classes read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:264)
at java.security.AccessController.checkPermission(AccessController.java:427)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
...

Hmm…this looks like we need some permissions granted. That‘s easy, let me go
the $JRE/lib/security directory and change the java.policy file
and add a new grant permission and it should all be fine. So I edit the
java.policy file and add the grant permission:

grant codeBase "file://dev/salsa/classes" {
permission java.io.FilePermission ">", "read";
};

I run the program thinking I fixed the problem for good. But not so fast! I
see the same exception again. Then it hit me again. I had committed another
silly error. It appears that I gave 2 slashes (file://…) to the codebase
to separate the protocol and the url.

So I quickly undo the changes I made to the java.policy file.
I go back to the codebase property and this time I set it as follows:

java.rmi.server.codebase=file:/dev/salsa/classes

I restart my RMI server and voila! Everything works fine this time!

Thought this record of my silly errors might help someone else sometime.

[J] Thanks to Jeremy Pitten
for reminding me the codebase settings.

YouRIt

Advertisements

Published by

deepakalur

Cofounder & CTO @ Chartcube.com