Mridang Agarwalla
Mridang Agarwalla

Reputation: 45018

Passing parameter from one process to another in Java

I'm writing a desktop Java application which basically runs in the system tray. This application can accept a command line parameter. If there's an instance of the application running and another instance is invoked with a command line argument, it simply passes it to the the running process in the system tray.

I'm very lost as to how to implement this? I've seen some apps do it, but I don't remember the name. All I'd like to pass is the string argument and then exit.

Upvotes: 4

Views: 2432

Answers (6)

jump3r
jump3r

Reputation: 161

Another option is to use cache that runs in a separate process.

Upvotes: 0

Subin Sebastian
Subin Sebastian

Reputation: 10997

You need to perform some sort of inter-process communication. There are numerous technologies that could be employed to achieve this, including:

  • sockets
  • webservices
  • Java Remote Method Invocation (RMI)
  • using a database
  • writing to a file

(Writing to a file is a poor choice for IPC unless you're using something like a POSIX named pipe rather than an ordinary file).

Upvotes: 0

xiaofeng.li
xiaofeng.li

Reputation: 8587

Basically the idea is to bind a local port on start up, if fails connects to it and send parameters.

public class StartOnce {

  public static void main(String args[]) throws IOException {
    SocketAddress addr = new InetSocketAddress("127.0.0.1", 9999);

    try {
        ServerSocket ss = new ServerSocket();
        // tries to bind to localhost:9999
        ss.bind(addr);
        // Ok, I'm the first instance, listen for other instances for update.
        while(true) {
            Socket s = ss.accept();
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            s.close();
        }
    } catch (BindException e) {
        // BindException, tries to send message to the first instance.
        System.out.println("Another instance is running. Say hi.");
        Socket s = new Socket();
        s.connect(addr);
        PrintWriter pw = new PrintWriter(s.getOutputStream());
        pw.println("Hello.");
        for (String arg : args) {
            pw.println(arg);
        }
        pw.close();
        s.close();
    }
  }
}

Upvotes: 2

AlexR
AlexR

Reputation: 115338

By other words you want your application to be a singleton. When user tries to run yet another instance of application it should send some kind of command to already existing one.

You can do it as following.

The application when started should open server socket to port you choose. If it succeeds it just should start. This is the first instance of your application. If if fails it means that the port is already busy, so other instance of your application is listening to it. In this case connect to this socket, send the command and terminate.

Be careful when choosing the port number. It should be high enough (>10000) and should not be used by any other popular application. Choose some number and then try to google it.

Upvotes: 2

Lai Xin Chu
Lai Xin Chu

Reputation: 2482

There are 2 parts to implementing this solution:

  1. Detecting whether there is already an instance of the program running
  2. Passing the String argument to a program if it is already running

Both 1 and 2 can be done at the same time using Sockets.

What you can do is to get your program to listen to a port when your program is started. When you start another instance of the program, it will attempt to listen to the same port, and if the port is occupied, you can presume that there is already an instance of the program running. Of course, you should choose a port that is not commonly in use by any application.

Then, if there is an instance of the program already started, simply use Sockets to pass the String argument over.

Upvotes: 2

Alex
Alex

Reputation: 25613

There is multiple ways to achieve this. In Java you can use JMS for such a thing. If you're using Spring there is templates for this. Here is a tutorial on how to get started using JMS.

Or you can use Sockets, or even RMI to exchange data within multiple process.

Upvotes: 0

Related Questions