Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI Example

 
JinXi Li
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For using rmi,I write a examples for test.But when I run server side program,there is error.Like:
HelloImpl err:access denied (java.net.SocketPermission 127.0.0.1:1099 connect,re
solve)
java.security.AccessControlException: access denied (java.net.SocketPermission 1
27.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:272)
at java.security.AccessController.checkPermission(AccessController.java:
399)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:545)
at java.lang.SecurityManager.checkConnect(SecurityManager.java:1044)
at java.net.Socket.<init>(Socket.java:262)
at java.net.Socket.<init>(Socket.java:100)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirect
SocketFactory.java:25)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMaster
SocketFactory.java:120)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:499)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:190
)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:174)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:318)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:160)
at examples.hello.HelloImpl.main(HelloImpl.java:17)

Who can help me to solve the problem.
Here is my code:
package examples.hello;
import java.rmi.*;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}

package examples.hello;
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
public class HelloImpl extends UnicastRemoteObject implements Hello {
private String name;
public HelloImpl(String s) throws RemoteException {
super();
name = s;
}
public String sayHello() throws RemoteException {
return "Hello Zeng Gang";
}
public static void main(String[] args) {
System.setSecurityManager(new RMISecurityManager());
try {
HelloImpl obj = new HelloImpl("HelloServer");
Naming.rebind("/HelloServer",obj);
System.out.println("HelloServer bound in registry");
}
catch(Exception e) {
System.out.println("HelloImpl err:"+e.getMessage());
e.printStackTrace();
}
}
}
Another question: How to use policy file?
Thanks!
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you didn't start the RMI Registry. You can use the policy file by giving the policy file as one of the configuration parameters during the RMI server startup like -D java.security.policy=java.policy
 
JinXi Li
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!
But I have started rmiregistry!
 
Sai Prasad
Ranch Hand
Posts: 560
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remove the code that sets the RMI Security Manager. If you like to use RMISecurityManager, you need to have a java.policy file which gives rights for the server to make the socket connection at port 1099
 
Terry Wang
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sample policy file:
grant {
permission java.security.AllPermission;
};
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd just remove the RMISecurity.
Mark
 
JinXi Li
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyone!
I have solved the problem with policy file!
Thanks again!
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler
bartender posted:
I'd just remove the RMISecurity.

Mark,
Do you think that the grader might take a few points off for not implementing the security?
Eugene Kononov.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark,
Do you think that the grader might take a few points off for not implementing the security?

No, just ask Peter.
I had security on the client only in my submission. and I didn't even need that.
Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic