ssbecse
ssbecse

Reputation: 113

How to communicate with existing java process

How can I pass some values from a shell script to a Java program that is already running?

Upvotes: 4

Views: 2053

Answers (4)

Paul Whelan
Paul Whelan

Reputation: 16809

See how to pass command line parameters to java here

EDIT: new information in the question means this is no longer a suitable solution

Upvotes: 0

Vladimir Dyuzhev
Vladimir Dyuzhev

Reputation: 18336

Standalone Java application is already running, so command line parameters are out of question.

Simplest alternatives remaining are polling for files, sockets and HTTP server.

Polling for files:

Make you java app to read a specific directory once in a few seconds. If a file appears in that directory, read it and do as it says. Make your shell script to form that file.

Socket:

Make you java app to listen on a socket. Use netcat or a similar utility to send commands to that socket.

HTTP Server:

Start an HTTP listener within the Java process. Use wget or similar utility to post your commands to that listener:

HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 5);
httpServer.createContext("/", new TileServerRequestHandler());
httpServer.setExecutor(Executors.newCachedThreadPool());
httpServer.start();

Upvotes: 5

DaveH
DaveH

Reputation: 7335

Write the params to a file that your process interrogates periodically? Crude, but it'd work ( but with a lot of usage limitations ). Or have your process listen on a socket and get your shell script to send the parameters down the socket.

Upvotes: 1

Isaac Truett
Isaac Truett

Reputation: 8874

One option for communicating with a Java process is the Java Messaging Service API. Your shell script could launch a JMS client to send messages to the main application.

Upvotes: 0

Related Questions