Suppose I have a java program, myProgram.jar
, which I have running on a server. To start the program I would type this into the terminal:
>java -jar myProgram.jar
and the program would continue to run indefinitely. Now what about if the program had a function such as
void processInput(String text){
//process the text
}
and I wanted to SSH into the server and call this function with a particular string? so I could log into my server at any time and alter the state of my program. Is this possible?
This can be done, but not easily.
There are standard ways to achieve what you probably want: MBeans. Take a look at http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html
You'll have a number of options here. The simplest would be if you only needed to provide your custom text as an argument at startup to the Java program - in which case you any arguments at the end of your java
command would be passed as a String array into your programs main
method.
Otherwise, you'll be looking to to implement some sort of remote procedure call (RPC). You could use something like Java RMI (remote method invocation) - where your main execution of your program starts, and you could use child executions of your program (or another client library all together) that calls methods within your main execution while it is still running. If you wanted to extend this further, you could have it host web services over standard HTTP, and use SOAP or REST calls.
There are many additional options and variations here, depending upon your exact requirements.
+1 for DagR's suggestion - again, depending upon exactly what you're looking to do, JMX may be a good fit for this as well.
You could implement a Java client application that calls methods on the Java application running on the server using RMI. Then when you ssh into the server you can run your client application with the parameters you need and have it call the methods in the other program.