0

How to translate the following conversation:

  • Application1: "Hello, Application2, I need the list of words to be analyzed"
  • Application2: "Here it is: String[]"

into Java code, where the two processes reside on the same machine?

I have seen this question and others related, but the answer is not really clear to me. Thus, I thought something like a listener could be useful. Moreover, I would prefer something really straightforward, that does not involve the usage of complicated frameworks (such as Spring), as suggested by previous questions.

Community
  • 1
  • 1
Eleanore
  • 1,750
  • 3
  • 16
  • 33
  • 3
    They are separate process, running in different JVMs or are they separate programs/classes running in different threads within the same JVM? – MadProgrammer Jul 08 '14 at 08:39
  • 3
    If we are talking about inter-process communications, then `Socket`s become a reality and *"something really straightforward"* takes a back seat... – MadProgrammer Jul 08 '14 at 08:40
  • It's not clear to me what you are trying to do. It's not difficult to pass information from one program to another--this is not necessarily a Java question and there are _many_ possible strategies. For instance, you can set up a fifo file and have a C program write data and have a Java program open and read the fifo to get information from it or vice versa and have the Java program write to a fifo and have some other process read it (and obviously you could do the same with two separate Java programs/processes). – Jared Jul 08 '14 at 08:58

3 Answers3

1

Java RMI (Remote Method Invocation) might do.

(No longer the rmic compiler is needed - should you still find such an obsolete sample.)

The newer version of RMI uses interfaces, an implementaion class and a port, for discovery.

I used it to implement a single instance class: if a second instance of an application is started, it discovers its class on a port, possible searches another port if already occupied, and delegates its command line to the first instance of itself.

Without administrator rights could be tickly.

Client application:

    public static void main(String[] args) {
        try {
            DictServer server = (DictServer)
                    Naming.lookup("//localhost:1024/dictserver");
            String[] words = server.getWords();
            System.out.println(Arrays.toString(words));
        } catch (RemoteException | MalformedURLException
                | NotBoundException ex) {
            ex.printStackTrace();
        }
    }

Common interface:

public interface DictServer extends Remote {
    public String[] getWords() throws RemoteException;
}

Server application:

public class DictServerImpl extends UnicastRemoteObject implements DictServer {
    public DictServerImpl() throws RemoteException {
    }

    @Override
    public String[] getWords() throws RemoteException {
        return new String[] { "unu", "du", "tri", "kvar", "kvin", "ses" };
    }

}

public class Main {
    public static void main(String[] args) {
        try {
            int port = 1024;
            Registry registry = LocateRegistry.createRegistry(port);
            registry.rebind("dictserver", new DictServerImpl());
        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }
}

Here used port 1024 and assuming running on same machine.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

You can make use of REST webservices.

One will ask for the data, and other will provide data on request.

Sending an array is also possible in JSONArray in webservice.

Also webservices will be useful in desktop,web and mobile applications.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
0

From your example its evident that Application 2 works more like a server and Application 1 works more like a client. With that said you can use Java Sockets to communicate between these two.

You need to Implement ServerSocket for Application 2 and Socket for Applilcation 1.

This is a pretty good trail to get you started.

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33