Register your code with rmi registry and start the server.
Following link have all the step to run sample rmi server
http://sumit-bhasin.talkmeblog.com/S...e-b1-p5536.htm
This is a discussion on RMI: Can not connect Client on a custom port - Java ; Hi all I have a problem with connecting client on a custom port (other then 1099) in my RMI application I've made the smallest possible test case which clarifies my problem. So my test consists of two classes (ServerImpl and ...
Hi all I have a problem with connecting client on a custom port (other
then 1099) in my RMI application
I've made the smallest possible test case which clarifies my problem.
So my test consists of two classes (ServerImpl and Client) and one
interface (IServer). In the example i'm trying to establish connection
on port 1100
// IServer interface
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface IServer extends Remote {
public int sum(int a, int b) throws RemoteException;
}
// ServerImpl class
package rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class ServerImpl implements IServer {
ServerImpl() throws RemoteException {
super();
}
public int sum(int a, int b) throws RemoteException {
return a + b;
}
public static void main(String args[]) throws RemoteException {
IServer server = new ServerImpl();
Registry registry = null;
final int port = 1100;
try {
registry = LocateRegistry.createRegistry(port);
} catch (RemoteException e) {
throw new RemoteException(String.format(
"Could not create registry on port: %d,"
+ " details have been logged", port));
}
Remote stub = UnicastRemoteObject.exportObject(server, port);
/* Try to bind the remote object in registry */
registry.rebind("rmi://127.0.0.1/MyServer", stub);
}
}
// Client class
package rmi;
import java.rmi.Naming;
public class Client {
public static void main(String[] args) {
String url = "rmi://127.0.0.1:1100/MyServer";
IServer remoteObject;
try {
remoteObject = (IServer) Naming.lookup(url);
System.err.println("Got remote object");
System.err.println(" 1 + 2 = " + remoteObject.sum(1, 2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
This example produces "java.rmi.NotBoundException: MyServer" I've
tried various combinations of things set in url variable that resides
in Client class with no luck. E.g. if I set it to String url =
"MyServer:1100";
I get "java.net.MalformedURLException: not a hierarchical URL:
MyServer:1100"
Right now I really do not know what else should I try... hope someone
can help me with this
Thanks,
Roman
Register your code with rmi registry and start the server.
Following link have all the step to run sample rmi server
http://sumit-bhasin.talkmeblog.com/S...e-b1-p5536.htm
On 13 ΣΕΞΤ, 23:47, Royan <romayan...@gmail.com> wrote:
> Hi all I have a problem with connecting client on a custom port (other
> then 1099) in my RMI application
>
> I've made the smallest possible test case which clarifies my problem.
> So my test consists of two classes (ServerImpl and Client) and one
> interface (IServer). In the example i'm trying to establish connection
> on port 1100
>
> // IServer interface
> package rmi;
>
> import java.rmi.Remote;
> import java.rmi.RemoteException;
>
> public interface IServer extends Remote {
> public int sum(int a, int b) throws RemoteException;
>
> }
>
> // ServerImpl class
> package rmi;
>
> import java.rmi.Remote;
> import java.rmi.RemoteException;
> import java.rmi.registry.LocateRegistry;
> import java.rmi.registry.Registry;
> import java.rmi.server.UnicastRemoteObject;
>
> public class ServerImpl implements IServer {
> ServerImpl() throws RemoteException {
> super();
> }
>
> public int sum(int a, int b) throws RemoteException {
> return a + b;
> }
>
> public static void main(String args[]) throws RemoteException {
> IServer server = new ServerImpl();
> Registry registry = null;
>
> final int port = 1100;
> try {
> registry = LocateRegistry.createRegistry(port);
> } catch (RemoteException e) {
> throw new RemoteException(String.format(
> "Could not create registry on port: %d,"
> + " details have been logged", port));
> }
> Remote stub = UnicastRemoteObject.exportObject(server, port);
>
> /* Try to bind the remote object in registry */
> registry.rebind("rmi://127.0.0.1/MyServer", stub);
> }
>
> }
>
> // Client class
> package rmi;
>
> import java.rmi.Naming;
>
> public class Client {
> public static void main(String[] args) {
> String url = "rmi://127.0.0.1:1100/MyServer";
> IServer remoteObject;
> try {
> remoteObject = (IServer) Naming.lookup(url);
>
> System.err.println("Got remote object");
>
> System.err.println(" 1 + 2 = " + remoteObject.sum(1, 2));
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> }
>
> This example produces "java.rmi.NotBoundException: MyServer" I've
> tried various combinations of things set in url variable that resides
> in Client class with no luck. E.g. if I set it to String url =
> "MyServer:1100";
>
> I get "java.net.MalformedURLException: not a hierarchical URL:
> MyServer:1100"
>
> Right now I really do not know what else should I try... hope someone
> can help me with this
>
> Thanks,
> Roman
The correct answer to my question is
registry = LocateRegistry.createRegistry(port); // for server
registry.rebind("MyServer", stub); // for server
Naming.lookup("rmi://myhost:1100/MyServer"); // for client