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)
Best Answer
2 things: