7

I'm working on Java RMI application, and having problem binding a server to the registry. I'm working on eclipse using rmi plugin, and everything worked fine before I had to format my pc. Also, I'm sure the code is ok since it's the one given to me as a solution, so there must be something wrong with my configuration. Here's the code:

public static void main (String[] args){

    try{

         RMIChatServer myObject = new RMIChatServerImpl();

        System.setSecurityManager(new RMISecurityManager());

        Naming.rebind("Hello", myObject);

        System.out.println("Remote object bound to registry");
    }
    catch( Exception e){

        System.out.println("Failed to register object " + e);
        e.printStackTrace();
        System.exit(1);

    }

}

Exceptions it gives:

Failed to register object java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: access to class loader denied
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is: 
    java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: access to class loader denied
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:419)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:273)
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:251)
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:377)
    at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
    at java.rmi.Naming.rebind(Naming.java:177)
    at RMIChatServerImpl.main(RMIChatServerImpl.java:175)
Caused by: java.rmi.UnmarshalException: error unmarshalling arguments; nested exception is: 
    java.lang.ClassNotFoundException: access to class loader denied
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:409)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:267)
    at sun.rmi.transport.Transport$1.run(Transport.java:177)
    at sun.rmi.transport.Transport$1.run(Transport.java:174)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:553)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:808)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:667)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.ClassNotFoundException: access to class loader denied
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:447)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:184)
    at java.rmi.server.RMIClassLoader$2.loadClass(RMIClassLoader.java:637)
    at java.rmi.server.RMIClassLoader.loadClass(RMIClassLoader.java:264)
    at sun.rmi.server.MarshalInputStream.resolveClass(MarshalInputStream.java:216)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1593)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1514)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1750)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    ... 13 more
Caused by: java.security.AccessControlException: access denied ("java.io.FilePermission" "\D:\uni\YEAR 3\Enterprise Programming\java\czat solution 2\bin\-" "read")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366)
    at java.security.AccessController.checkPermission(AccessController.java:555)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at sun.rmi.server.LoaderHandler$Loader.checkPermissions(LoaderHandler.java:1176)
    at sun.rmi.server.LoaderHandler$Loader.access$000(LoaderHandler.java:1130)
    at sun.rmi.server.LoaderHandler.loadClass(LoaderHandler.java:411)
    ... 22 more

I researched the problem and most say that it's because of codebase settings (I also use security policy), I have tried different settings, and currently using 'compute from classpath' option provided by rmi plugin, which worked before, but fails now :( Please advice!

tomsky
  • 535
  • 4
  • 11
  • 28
  • Have you tried to not set the 'RMISecurityManager'? – srkavin Nov 16 '11 at 19:25
  • Run it with `-Djava.security.debug=access,failure` and see if anything jumps at you – gotomanners Nov 16 '11 at 19:31
  • 1
    yes, I'm on windows 7, when I run it without security manager, it gives me: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve") at java.security.AccessControlContext.checkPermission(AccessControlContext.java:366) – tomsky Nov 16 '11 at 20:48
  • It says: -Djava.security.debug is not recognized as an internal or external command... – tomsky Nov 16 '11 at 20:51
  • @tomsky if you are getting `AccessControlExceptions` you are definitely under the control of a `SecurityManager`, whether you think so or not. `-Djava.security.debug=access,failure` is a parameter to the `java` command, not a command of its own. – user207421 Nov 16 '11 at 23:18
  • possible duplicate of [RMI Registry Issue: rmiregistry may cause unintended exceptions when binding with codebase using the "file:" URL scheme](http://stackoverflow.com/questions/8152838/rmi-registry-issue-rmiregistry-may-cause-unintended-exceptions-when-binding-wit) – user207421 Nov 16 '11 at 23:33

5 Answers5

10

FWIW

I upgraded from JDK1.6.0_33 to 1.7.0_21 and had the same problem. I found this document and resolved the problem by starting rmiregistry with:

rmiregistry -J-Djava.rmi.server.useCodebaseOnly=false

Stedy
  • 7,359
  • 14
  • 57
  • 77
user1766585
  • 101
  • 1
  • 2
3

So, quick summary of the problem and the solution:

If you're running on JDK 7.1/ 6.29 (possible some other version) RMI server will not bind if you set its codebase to the file or directory on your hard disk. The same code works just fine under older version of JDK (tested on 6.24). Thanks for your help!

Tim Post
  • 33,371
  • 15
  • 110
  • 174
tomsky
  • 535
  • 4
  • 11
  • 28
2

The underlying problem here is that the RMI Registry is running under a SecurityManager and its .policy file doesn't grant java.io.FilePermission" "\D:\uni\YEAR 3\Enterprise Programming\java\czat solution 2\bin\-" "read".

The clue to that is that this is a ServerException, i.e. thrown at the target of the call, and that the call itself is rebind().

See this post for an explanation.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks, this explains a lot. The easiest thing to do in my case was just to use older version of JDK. – tomsky Nov 17 '11 at 21:21
1

Yes this problem occurs in Java 1.7 and above. So always go through the enhancements document if you upgrade your Java version. It is a fairly simple solution. You just need to start the rmiregistry in a different way.

Go through this document- http://docs.oracle.com/javase/7/docs/technotes/guides/rmi/enhancements-7.html

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
0

Have you tried to explicitly give access to the file using the *.policy file? as described on http://docs.oracle.com/javase/7/docs/technotes/guides/security/permissions.html

grant codebase "file:/path/to/code" {
    permission java.io.FilePermission "\D:\uni\YEAR 3\Enterprise Programming\java\czat solution 2\bin\-", "read";
}

I have the same issue with openjdk... probably caused by this commit: http://hg.openjdk.java.net/jdk7u/jdk7u-gate/jdk/rev/7ed2fd310470

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
asura
  • 11
  • 1