Rmi 连接与 localhost 被拒绝
我在使用 java rmi 时遇到问题:
当我尝试运行服务器时,出现 connectException(见下文)。
执行rebind方法时发生异常:
Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);
当使用rmi://localhost:2020/RemoteDataPointHandler代替时,它也不起作用。使用默认端口也不起作用。我也尝试使用 127.0.0.1 ip 地址,但效果相同。
我的运行时参数:
-Djava.security.policy=java.security.AllPermission
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) at java.rmi.Naming.rebind(Naming.java:160) at be.fortega.knx.server.Main.(Main.java:25) at be.fortega.knx.server.Main.main(Main.java:16) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433) at java.net.Socket.connect(Socket.java:524) at java.net.Socket.connect(Socket.java:474) at java.net.Socket.(Socket.java:371) at java.net.Socket.(Socket.java:184) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569) ... 7 more
I have a problem using java rmi:
When I'm trying to run my server, I get a connectException (see below).
Exception happens when executing the rebind method:
Runtime.getRuntime().exec("rmiregistry 2020");
MyServer server = new MyServer();
Naming.rebind("//localhost:2020/RemoteDataPointHandler", server);
when using rmi://localhost:2020/RemoteDataPointHandler instead, it doesn't work either. Also using the default port does not work. I also tried using the 127.0.0.1 ip-address, but with the same effect.
my runtime args:
-Djava.security.policy=java.security.AllPermission
Exception in thread "main" java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: java.net.ConnectException: Connection refused at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574) at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185) at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171) at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:306) at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source) at java.rmi.Naming.rebind(Naming.java:160) at be.fortega.knx.server.Main.(Main.java:25) at be.fortega.knx.server.Main.main(Main.java:16) Caused by: java.net.ConnectException: Connection refused at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:433) at java.net.Socket.connect(Socket.java:524) at java.net.Socket.connect(Socket.java:474) at java.net.Socket.(Socket.java:371) at java.net.Socket.(Socket.java:184) at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22) at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128) at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:569) ... 7 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该连接异常也有类似的问题。当注册表尚未启动(如您的情况)或注册表已未导出(如我的情况)时,它会被抛出。
但对启动注册表的两种方法之间的差异进行简短评论:
在新进程中运行 javas bin 目录中的 rmiregistry.exe 并继续与 java 代码并行。
rmi 方法调用启动注册表,返回对该注册表远程对象的引用,然后继续下一条语句。
在您的情况下,当您尝试绑定对象时,注册表没有及时启动
had a simliar problem with that connection exception. it is thrown either when the registry is not started yet (like in your case) or when the registry is already unexported (like in my case).
but a short comment to the difference between the 2 ways to start the registry:
runs the rmiregistry.exe in javas bin-directory in a new process and continues parallel with your java code.
the rmi method call starts the registry, returns the reference to that registry remote object and then continues with the next statement.
in your case the registry is not started in time when you try to bind your object
任何人替换时,它似乎有效,
当我用
为什么?有什么区别?
It seems to work when I replace the
by
anyone an idea why? What's the difference?
在尝试连接(注册)RMI 服务之前,您需要先运行
rmiregistry
。LocateRegistry.createRegistry(2020)
方法调用在指定端口号上创建并导出注册表。请参阅 LocateRegistry 的文档
You need to have a
rmiregistry
running before attempting to connect (register) a RMI service with it.The
LocateRegistry.createRegistry(2020)
method call creates and exports a registry on the specified port number.See the documentation for LocateRegistry
我们在 Windows 中可以注意到的一个区别是:
如果您使用 Runtime.getRuntime().exec("rmiregistry 1024");
您可以看到 rmiregistry.exe 进程将在任务管理器中运行
,而如果您使用
Registryregistry = LocateRegistry.createRegistry(1024);
你看不到任务管理器中运行的进程,
我认为Java以不同的方式处理它。
这是我的 server.policy 文件
在运行应用程序之前,请确保您杀死了所有现有的
javaw.exe 和 rmiregistry.exe 对应于您的 rmi 程序,它们是
已经运行了。
以下代码通过使用
Registry.LocateRegistry()
或VM 参数
代码对我有用:
One difference we can note in Windows is:
If you use
Runtime.getRuntime().exec("rmiregistry 1024");
you can see rmiregistry.exe process will run in your Task Manager
whereas if you use
Registry registry = LocateRegistry.createRegistry(1024);
you can not see the process running in Task Manager,
I think Java handles it in a different way.
and this is my server.policy file
Before running the the application, make sure that you killed all your existing
javaw.exe and rmiregistry.exe corresponds to your rmi programs which are
already running.
The following code works for me by using
Registry.LocateRegistry()
orVM argument
Code:
看来你应该将你的命令设置为
String[]
,例如:它就像
main(String[] args)
的样式。it seems that you should set your command as an
String[]
,for example:it just like the style of
main(String[] args)
.