这段代码不起作用。嗯,它可以工作,但客户端不打印。我做错了什么吗?我正在使用 W7 和 NetBeans。有没有办法查看 rmiregistry 是否正常?如果我删除这些捕获,我无法编译它。我很确定问题来自重新绑定和查找。
服务器新.java:
interface C extends Remote
{
public String now() throws RemoteException ;
}
class Server extends UnicastRemoteObject implements C
{
public String now() throws RemoteException
{
return "99:99";
}
Server() throws RemoteException
{
}
}
public class ServerNew
{
public static void main(String[] args) throws Exception
{
Server server=new Server();
System.setSecurityManager(new java.rmi.RMISecurityManager());
LocateRegistry.createRegistry(1099);
Naming.rebind("rmi://localhost:1099/time", server);
}
}
客户新.java:
interface C extends Remote
{
public String now() throws RemoteException ;
}
public class ClientNew
{
public static void main(String[] args) throws Exception
{
C server=(C) Naming.lookup("rmi://localhost:1099/time");
String text=server.now();
System.out.println(text);
}
}
服务器的输出(ServerNew.java:38
Naming.rebind
):
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
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 java.lang.SecurityManager.checkConnect(SecurityManager.java:1051)
at java.net.Socket.connect(Socket.java:574)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at java.rmi.Naming.rebind(Naming.java:177)
at servernew.ServerNew.main(ServerNew.java:38)
Exception in thread "RMI TCP Connection(idle)" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:61881" "accept,resolve")
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 java.lang.SecurityManager.checkAccept(SecurityManager.java:1174)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.checkAcceptPermission(TCPTransport.java:654)
at sun.rmi.transport.tcp.TCPTransport.checkAcceptPermission(TCPTransport.java:293)
at sun.rmi.transport.Transport$1.run(Transport.java:176)
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)
有了这个异常处理,任何人都不可能回答你的问题。我建议您修复它并提供正在抛出的异常。到目前为止,您发布的内容完全没有意义,不应该被写出来。