Question

I would like to implement a command line interface for a Java application. This wouldn't be too difficult to do, except I would like the command line program to affect the state of another Java GUI program. So for example, I could type:

java CliMain arg1 arg2

And another running GUI instance would perform an appropriate action.

What is the easiest way of implementing something like this?

Was it helpful?

Solution

You could have the GUI application listen on a TCP port (on the localhost interface) and the CLI application would connect to it.

One way to do this would be to use REST over HTTP.

OTHER TIPS

Another way is through JMX. It gives you a lot of stuff "for free" (in the simple case you just implement a bean and register it -- very simple), and is particularly well suited to this task.

you can have the GUI application(like an editor) listen on

1) clipboard event of a certain type
if the event is of a type that you are interested in, then get the clipboard contents.

2) server socket on a certain port
listen on a server socket. when the CLI program starts, it connects to the server socket at a known port, sends info and quits.

3) queue
you can enque from the CLI program and deque from the GUI program.

if you want to investigate further, many professional editors like emacs use the same mechanism. http://www.emacswiki.org/emacs/EmacsClient

Your application could be controlled via RMI. The application would implement a control interface, register its service on localhost and the command line application would get an rmi proxy and call the desired control methods...

Seems hard at first, but when you've tried out you'll quickly see how easy that is. And it also supports encryption via SSL. So you could secure your data exchange if there was security relevant data online.

The easiest way would be for the GUI to listen for commands on a TCP port. The command line would send commands, and the GUI would interpret them.

Maybe you could do it with named pipes as well, but I'm not sure how you'd go about implementing that in Java.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top