tags:

views:

908

answers:

5

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?

+2  A: 

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.

Greg Hewgill
This is a very powerful technique. This also allows you to provide a HTTP interface to your program, useful for immediately making your app network accessible.
Jamie Love
A: 

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.

Will Hartung
A: 

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

anjanb
A: 

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.

dhiller
A: 

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.

Claudiu