0

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];
user207421
  • 305,947
  • 44
  • 307
  • 483
anie
  • 399
  • 5
  • 17
  • 3
    So, there are no (command line) arguments. how do you execute your application, and what parameters do you pass? Basically, what that error tells you is that you are trying to extract the first element of an empty array – Stultuske May 07 '21 at 05:35
  • @Stultuske i ma actually using netbeans so in netbeans when create a project and create files then just right click on file and it press run. for this it is consist of three files server, client, and interface. – anie May 07 '21 at 05:43
  • 1
    which is probably why your args is empty. since you can't pass command line arguments, you'll have to configure netbeans to pass the right variables, or just provide the information in another way, otherwise you'll have the exact same issue the moment you run this outside of netbeans – Stultuske May 07 '21 at 05:59
  • @Stultuske could you please show me how that could be fixed – anie May 07 '21 at 06:05
  • 1
    you want to learn how to do the basics after you've written a network application? you can use a Scanner instance, a properties file, JOptionPane dialogs, ... – Stultuske May 07 '21 at 06:09
  • 1
    This problem occurs before you have executed one line of RMI code. It therefore has nothing whatseover to do with [tag:RMI]. – user207421 May 07 '21 at 06:50

2 Answers2

1

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

Lahiru Wijesekara
  • 623
  • 1
  • 10
  • 22
0

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.

Bishnu Das
  • 161
  • 2
  • 14
  • as was already pointed out, the OP uses netbeans and has no idea what command line arguments are. Which means just filling them in netbeans is a bad idea, since the moment (s)he will try to execute it outside of netbeans, the problem will just reappear – Stultuske May 07 '21 at 07:03