1

The time issue doesn't really matter. But i'm wondering how to make some external calls to a java program that is already running.

A simple example: An infinite loop that does some processing on files.

If i had such a program described above, how could i write a shell/perl/python/etc script that would allow me to stop, start, pause, the java program?

rage
  • 1,777
  • 5
  • 25
  • 36

5 Answers5

1

Unless your Java application explicitly enables to access it externally (e.g. from RMI, SOAP/REST-WebService, MQ, ...) there is no direct way to access your Java program.

The simplest (but certainly not the best) solution is to use the File System as in between. Your shell script creates a file which is picked up by the Java application. E.g create a file pause; if the app sees this file exists it will delete it and pause. Stopping the app is simply killing the process.

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
1

This is not really easy but you can use the RMI to access controlling class that expose an API to control your application.

http://docs.oracle.com/javase/tutorial/rmi/index.html

frenchfrie
  • 111
  • 6
  • So if I used RMI.. i would be able to write several Perl scripts like start.pl, stop.pl, etc that would essentially just make a call to the client java program? – rage Mar 03 '14 at 17:35
  • you can make a client program in java wich takes theses as arguments (or several no-args programs) that call RMI methods. I do not know about perl but you can probably call the java client(s) with it. – frenchfrie Mar 04 '14 at 09:16
1

The simplest way is to write a script that starts the program on 'start', and send a SIGTERM signal to the the Java process on 'stop'. Your code can handle the signal accordingly for clean shutdown.

This will be sufficient if it's just a start/stop script scenario. For more advanced behaviour, the other answers mention other alternatives which require networking.

Emerson Farrugia
  • 11,153
  • 5
  • 43
  • 51
  • This in general does not answer the question "how to make some external calls to a java program that is already running": start and stop are just examples – robermann Mar 03 '14 at 15:26
  • 1
    So you down-voted my answer and then added it as a comment to your own? That's just lovely, isn't it? My answer is valid. Pretty much every launch and service script for daemons in the UNIX world works this way, and he's free to send whatever signal he wants. I just gave him examples for two of the cases he mentioned. – Emerson Farrugia Mar 03 '14 at 15:31
  • First I wrote about "signals" before than you, but it is not the point :) The point is, how a Java program can handle external "interrupts" sent by script? – robermann Mar 03 '14 at 15:35
  • No, the point is twofold; (1) it's fine as an answer if it was fine enough for you to comment on, and (2) RTFM: "Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect." If my answer isn't good enough, it won't get voted up. As for signal handling, for his example, all he needs is a registered shutdown hook. SignalHandlers are there for weirder workloads. – Emerson Farrugia Mar 03 '14 at 15:39
  • Thanks for your efforts of explaining me the netiquette of this place man :) Of course +1 – robermann Mar 03 '14 at 15:58
0

A way could be that the main program, in an ad-hoc thread control, listen in a TCP socket for external commands, while the control script should write to that socket.

robermann
  • 1,722
  • 10
  • 19
  • It is also possible "catch" signals from unix os, although by a sort of hack: http://stackoverflow.com/questions/1409165/signal-handling-using-term – robermann Mar 03 '14 at 15:22
0

There are many ways a program can interact with the outside world. The commonest way nowadays in the Java world is to make your program a TCP server.

One common way is to use an embedded HTTP server such as Jetty. Your program would start Jetty in its own thread before going into its task. You can write servlets that interact with your program. You can use a Web browser to talk to it, or an HTTP client tool like cURL, or an HTTP client library for whatever language you like.

slim
  • 40,215
  • 13
  • 94
  • 127