while Running RMI using java and CMD. java.rmi.server.ExportException is displayed. The Exception is that, port is already in use.
The server interface
import java.rmi.*;
public interface AdditionInterface extends Remote {
public int add(int a, int b) throws RemoteException;
}
The Interface implementation
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
The Server Registry class
package chapter40;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionServer {
public static void main(String[] args) {
try {
Registry registry = LocateRegistry.getRegistry();
AdditionInterface obj = new Addition();
registry.rebind("Addition", obj);
System.out.println("Addition Server is ready");
} catch (Exception ex) {
System.out.println("Addition Server failed" + ex);
}
}
}
The client program
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class AdditionClient {
public static void main(String[] args) {
try {
String host="";
Registry registry = LocateRegistry.getRegistry(host);
AdditionInterface hello = (AdditionInterface) registry.lookup("Addition");
int result = hello.add(9, 2);
System.out.println("Result is: " + result);
} catch (Exception ex) {
System.out.println("HelloClient Exception" + ex);
}
}
}
I separated the server from the client classes in two different projects for clarity sake
I searched through various threads and finally got this to work in a way different from [ but also a combination from what I'd seen ]
RUN THE SERVER PROGRAM THUS (from the command prompt):
change the directory to the source folder for the server
cd c:\Users\Heavenly\workspace\RMIServerSide\src
compile the classes
javac chapter40/*.java
start the RMI registry
start rmiregistry
start the registry server class containing the main() method. NOTE THE LINE OF CODE IS SLIGHTLY DIFFERENT
start java -cp . chapter40.AdditionServer
(the server is now set)
RUN THE CLIENT PROGRAM
Change the directory to the source folder
cd c:\Users\Heavenly\workspace\RMIClientSide\src
compile all the classes
javac chapter40/*.java
3.Run the client program
java chapter40.AdditionClient
(everything is running now)
Related
I have three java files one is RMI server and RMI client and interface file, as follow:
server:
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends
java.rmi.server.UnicastRemoteObject implements ReceiveMessageInterface{
String address;
Registry registry;
public void receiveMessage(String x) throws RemoteException{
System.out.println(x);
}
public RmiServer() throws RemoteException{
try{
address = (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
System.out.println("can't get inet address.");
}
int port=3233;
System.out.println("this address=" + address + ",port=" + port);
try{
registry = LocateRegistry.createRegistry(port);
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[]){
try{
RmiServer s = new RmiServer();
}
catch (Exception e){
e.printStackTrace();
System.exit(1);
}
}
}
client:
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient{
static public void main(String args[]){
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println
("sending " + text + " to " +serverAddress + ":" + serverPort);
try{
registry=LocateRegistry.getRegistry
(serverAddress,(new Integer(serverPort)).intValue());
rmiServer=(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
interface:
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote{
void receiveMessage(String x) throws RemoteException;
}
so basically, when i run the server it will give my laptop address and port that is running in and this is working very well
however, the problem is when i run the client after i run the server it keep throwing this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 0 out of bounds for length 0
which client file in where this line:
String serverAddress=args[0];
If you look at the method "static public void main(String args[])", String args[] is a standard convention. You should pass values to this array at the command line when you invoke your program in the manner you have used.
The reason for your exception is, you don't have args[0] in your array as you have not passed a value. Make sure to pass three values via the command line when you invoke the program as you have accessed args[0], args[1], and args[2] in your program.
Example :
public class X{
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}
How to run
java X a ab abc
Expected outcome
a
ab
abc
As there is no argument value passed to the main method that's why it's throwing error.
I believe you are not assigning the command line arguments to the main method.
If you are using any IDE to run the project then search how to pass arguments during project execution.
Here you can find how to configure arguments for Netbeans IDE - Netbeans how to set command line arguments in Java
If you are using any other IDE then similarly search for that.
Otherwise, you can simply provide command-line arguments to the program by running via command prompt.
I am very new to Java and the RMI system. I am following a tutorial but I am unsure why I keep getting the following errors[1]
[1]: https://i.stack.imgur.com/xeYTn.png
I have attached the code (taken directly from the tutorial here: https://docs.oracle.com/javase/1.5.0/docs/guide/rmi/hello/hello-world.html)
I have tried:
removing any lines with 'package'
changing classpath variables
reinstalling java and javac
setting classpath in the 'rmiregistry &' command
Any help would be appreciated
edit: forgot to attach the code.
Hello.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote {
String sayHello() throws RemoteException;
}
Client.java
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client {
private Client() {
}
public static void main(String[] args) {
String host = (args.length < 1) ? null : args[0];
try {
Registry registry = LocateRegistry.getRegistry(host);
Hello stub = (Hello) registry.lookup("Hello");
String response = stub.sayHello();
System.out.println("response: " + response);
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
}
}
Server.java
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server implements Hello {
public Server() {
}
public String sayHello() {
return "Hello, world!";
}
public static void main(String args[]) {
try {
Server obj = new Server();
Hello stub = (Hello) UnicastRemoteObject.exportObject((Remote) obj, 0);
// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
You have to run the command rmiregistry & in the folder the code is compiled into. In this case, "files"
the "getting started" tutorial didn't mention that.
hi i am using this codes for rmi
RmiServer.java
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.net.*;
public class RmiServer extends java.rmi.server.UnicastRemoteObject
implements ReceiveMessageInterface
{
int thisPort;
String thisAddress;
Registry registry; // rmi registry for lookup the remote objects.
// This method is called from the remote client by the RMI.
// This is the implementation of the gReceiveMessageInterfaceh.
public void receiveMessage(String x) throws RemoteException
{
System.out.println(x);
}
public RmiServer() throws RemoteException
{
try{
// get the address of this host.
thisAddress= (InetAddress.getLocalHost()).toString();
}
catch(Exception e){
throw new RemoteException("can't get inet address.");
}
thisPort=3232; // this port(registryfs port)
System.out.println("this address="+thisAddress+",port="+thisPort);
try{
// create the registry and bind the name and object.
registry = LocateRegistry.createRegistry( thisPort );
registry.rebind("rmiServer", this);
}
catch(RemoteException e){
throw e;
}
}
static public void main(String args[])
{
try{
RmiServer s=new RmiServer();
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
RmiClient.java
import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;
public class RmiClient
{
static public void main(String args[])
{
ReceiveMessageInterface rmiServer;
Registry registry;
String serverAddress=args[0];
String serverPort=args[1];
String text=args[2];
System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
try{
// get the �gregistry�h
registry=LocateRegistry.getRegistry(
serverAddress,
(new Integer(serverPort)).intValue()
);
// look up the remote object
rmiServer=
(ReceiveMessageInterface)(registry.lookup("rmiServer"));
// call the remote method
rmiServer.receiveMessage(text);
}
catch(RemoteException e){
e.printStackTrace();
}
catch(NotBoundException e){
e.printStackTrace();
}
}
}
ReceiveMessageInterface.java
import java.rmi.*;
public interface ReceiveMessageInterface extends Remote
{
public void receiveMessage(String x) throws RemoteException;
}
This works fine normally , but when the a computer is connected to internet through mobile or it shares internet from other pc it doesn't work
I get this error.
java.net.connectexception connection timeout
when i tried to telnet it fails to connect but when i try to run this program that pc
to my pc it works.
Please let me know how to solve this issue.
Sounds like a firewall or proxy server issue.
i am having a problem creating RMIserver class because i keep getting this error : access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
now i read on google something about cerating new policy file but i dont really understand how to do so, can please someone help me ?
here is my server code :
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.RMISecurityManager;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.*;
import java.security.Permission;
import java.security.Security;
public class RmiServer extends UnicastRemoteObject
implements RmiServerIntf
{
public static final String MESSAGE = "Hello world";
public RmiServer() throws RemoteException
{
}
public String getMessage()
{
return MESSAGE;
}
public static void main(String args[])
{
System.out.println("RMI server started");
// Create and install a security manager
if (System.getSecurityManager() == null)
{
System.setSecurityManager(new RMISecurityManager());
System.out.println("Security manager installed.");
}
else
{
System.out.println("Security manager already exists.");
}
try
{ //special exception handler for registry creation
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created.");
}
catch (RemoteException e)
{
//do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}
try
{
//Instantiate RmiServer
RmiServer obj = new RmiServer();
// Bind this object instance to the name "RmiServer"
Naming.rebind("//localhost/RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
catch (Exception e)
{
System.err.println("RMI server exception:" + e);
e.printStackTrace();
}
}
}
You are using a SecurityManager (why?) but your security policy doesn't grant you the permission specified in the exception. You don't need a SecurityManager at all unless you are planning to use the RMI codebase feature: are you?
You really need to read the manual of RMI, and how to create a policy file:
http://docs.oracle.com/javase/tutorial/rmi/running.html
i m creating a Client/Server application in which my server and client can be on the same or on different machines but both are under ISP.
My RMI programs:-
-Remote Intreface:-
//Calculator.java
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
Remote Interface Implementation:-
//CalculatorImpl.java
public class CalculatorImpl
extends
java.rmi.server.UnicastRemoteObject
implements Calculator {
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}
public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}
Server:-
//CalculatorServer.java
import java.rmi.Naming;
import java.rmi.server.RemoteServer;
public class CalculatorServer {
public CalculatorServer(String IP)
{
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://"+IP+":1099/CalculatorService", c);
} catch (Exception e)
{
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer(args[0]);
}
}
Client:-
//CalculatorClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)Naming.lookup("rmi://"+args[0]+"/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
when both Server and client programs are on same machine:-
i start my server program by passing my router static IP address:-192.168.1.35
in args[0] and my server starts...fine.
and by passing the same Static IP address in my Client's args[0] also works fine.
but:-
when both Server and client programs are on different machines:-
now,i m trying to start my Server Program by passing it's public IP address:59.178.198.247 in args[0] so that it can recieve call over internet.
but i am unable to start it.
and the following exception occurs:-
Trouble: java.rmi.ConnectException: Connection refused to host: 59.178.198.247;
nested exception is:
java.net.ConnectException: Connection refused: connect
i think it is due to NAT Problem because i am under ISP.
so,my problem is that how can i start my RMI Server under ISP so that it can recieve remote calls from internet????
Presumably your ISP is blocking the connection. You'll have to talk to them.
Try to see if your port (1099) is visible (you can use this for example). If not, use an open port, or edit your firewall or router configuration (you might need to set up port forwarding). If it is, look into your RMI security policy.