Java RMI、Tomcat问题!
我正在学习 Java RMI,并有一个示例代码来展示如何使用 RMI 通过网络将对象传递到另一个虚拟机。
这是我正在使用的类和代码;
//interface
import java.rmi.*;
public interface MyRemote extends Remote
{
public String sayHello() throws RemoteException;
}
//My Remote Implementation Class
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Server says, 'Hey' ";
}
public MyRemoteImpl() throws RemoteException
{
}
public static void main (String[] args)
{
try
{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//My Remote Client Class
import java.rmi.*;
public class MyRemoteClient
{
public static void main (String [] args)
{
new MyRemoteClient().go();
}
public void go()
{
try{
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/Remote Hello");
String s = service.sayHello();
System.out.println(s);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
我不断收到 java.net.MalformedURLException: invalid URL String: rmi://127.0.0.1/Remote Hello 我知道这是由于 'Naming.lookup("rmi://127.0.0.1/Remote Hello")' 代码造成的在最后一堂课。
我已经安装了 tomcat 并尝试将文件放在一个目录中,试图让它在我的本地计算机上运行,但我没有感到高兴。
我已经为我的项目创建了一个 JAR 文件,并尝试将其放入一个目录中,但仍然没有成功。我听说过战争文件,但目前我正在学习的书还没有涉及到这一点...
有关如何在不调用异常的情况下解决此问题的任何提示?我是否将文件错误地放置在 Tomcat 中?我已将它们放在我的 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps 目录中。
任何帮助或提示将不胜感激。
更新
感谢您的建议,字符串异常似乎已经消失,但现在我对此感到困惑。
java.rmi.ConnectException:连接拒绝主机:127.0.0.1;嵌套异常是: java.net.ConnectException:连接被拒绝:连接 在 sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619) 在 sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:216) 在 sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:202) 在 sun.rmi.server.UnicastRef.newCall(UnicastRef.java:340) 在 sun.rmi.registry.RegistryImpl_Stub.lookup(来源未知) 在 java.rmi.Naming.lookup(Naming.java:101) 在 MyRemoteClient.go(MyRemoteClient.java:13) 在 _SHELL57.run(_SHELL57.java:10) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:623) 在 bluej.runtime.ExecServer$3.run(ExecServer.java:774) 原因:java.net.ConnectException:连接被拒绝:连接 在 java.net.DualStackPlainSocketImpl.connect0(本机方法) 在 java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) 在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:316) 在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:177) 在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:164) 在 java.net.PlainSocketImpl.connect(PlainSocketImpl.java:154) 在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:352) 在 java.net.Socket.connect(Socket.java:569) 在 java.net.Socket.connect(Socket.java:519) 在 java.net.Socket.(Socket.java:416) 在 java.net.Socket.(Socket.java:199) 在 sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:40) 在sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:146) 在 sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
I am learning about Java RMI and have an example code to show how RMI can be used to pass objects to another virtual machines over a network.
Here are the classes and code I am using;
//interface
import java.rmi.*;
public interface MyRemote extends Remote
{
public String sayHello() throws RemoteException;
}
//My Remote Implementation Class
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote
{
public String sayHello()
{
return "Server says, 'Hey' ";
}
public MyRemoteImpl() throws RemoteException
{
}
public static void main (String[] args)
{
try
{
MyRemote service = new MyRemoteImpl();
Naming.rebind("Remote Hello", service);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
//My Remote Client Class
import java.rmi.*;
public class MyRemoteClient
{
public static void main (String [] args)
{
new MyRemoteClient().go();
}
public void go()
{
try{
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/Remote Hello");
String s = service.sayHello();
System.out.println(s);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
I keep getting java.net.MalformedURLException: invalid URL String: rmi://127.0.0.1/Remote Hello which I know is due to the 'Naming.lookup("rmi://127.0.0.1/Remote Hello")' code in the last class.
I have installed tomcat and tried placing the files in a directory, trying to get it to run on my local machine but I have had no joy.
I have created a JAR file of my project and tried placing that in a directory also and still no luck. I have heard about war files but currently the book I am learning from hasnt come to that yet...
Any tips on how I can get around this without the exception being called? Am I placing the files incorrectly in Tomcat? I have placed them in my C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps directory.
Any help or tips will be greatly appreciated.
UPDATE
Thanks for the advice, the String exception seems to have gone away but now I am lumbered with this.
java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
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.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:101)
at MyRemoteClient.go(MyRemoteClient.java:13)
at _SHELL57.run(_SHELL57.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:623)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:316)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:177)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:164)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:154)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:352)
at java.net.Socket.connect(Socket.java:569)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.(Socket.java:416)
at java.net.Socket.(Socket.java:199)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两件事:
2 things:
Tomcat 和 RMI 有什么关系? Tomcat 是一个 Servlet/JSP 引擎,默认在端口 8080 上侦听 HTTP 请求。
RMI 是一个完全不同的协议,与 HTTP 无关。 RMI 守护程序默认使用端口 1099。
我建议查看这个。
What does Tomcat have to do with RMI? Tomcat is a servlet/JSP engine that listens for HTTP requests on port 8080 by default.
RMI is a totally different protocol that has nothing to do with HTTP. The RMI daemon uses port 1099 by default.
I'd recommend looking at this.
检查服务器上的 /etc/hosts 文件。它应该将 localhost 映射到 127.0.0.1,并将您的真实主机名映射到您的真实 IP 地址。如果您的配置需要相反的方式,请在导出远程对象之前将系统属性 java.rmi.server.hostname 设置为服务器 JVM 上的正确 IP 地址。
Check your /etc/hosts file at the server. It should map localhost to 127.0.0.1 and your real hostname to your real IP address. If you have a configuration where you need the other way round, set the system property java.rmi.server.hostname to the correct IP address at the server JVM before exporting the remote object.