Execute Java program server-side from a Node.js / Heroku app? - java

Can I call a Java program from a Node.js application on Heroku?
I have a Node.js/Heroku app. But now need to add server-side capability to run an algorithm on an input data file and output data to a JSON format. I already have a Java library that can read the file and run the algorithm, and it would be very difficult (at best) for me to re-write it in pure Node.js.
So, could write a command line program, that takes an input file and pipes the results to stdout, e.g.
java mytask.class -cp ./mylibrary.jar --in /tmp/file.in > output.json
Is it possible to shell out a call to a Java command line program from Node.js? I know one can deploy Java applications to Heroku, but here want to execute a bit of Java from a Node.js app.

Don't you want this and child_process.exec() in particular ?
Node provides a tri-directional popen(3) facility through the
child_process module.
It is possible to stream data through a child's stdin, stdout, and
stderr in a fully non-blocking way.
Note that your example command above isn't right, since you're trying to pipe to a file (output.json). Pipes only work between processes. The child process module would allow you to read the processes' stdout directly and you wouldn't need the file (similarly for the input stream)

Related

How can Java Applications communicate with MATLAB

I have some mex files that urgently need to be called via MATLAB, there is currently no way around. However, I really despise MATLAB's GUI (in)possibilities and would like to create some e.g. JavaFX Apps.
My question: how can a Java app's communicate with a running MATLAB instance?
I know that you can include Java objects into MATLAB, however I would prefere to have a standalone Java app.
Java can execute commands via command line for example:
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
So it is possible to execute a MATLAB script via command line in Java.
In MATLAB it is possible to write files with any data needed. I don't remember the exact way you may do this. http://www.mathworks.com/help/matlab/ref/fprintf.html gives an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
It is some kind of a workaround but it should work and it is not really hard to implement.
Update.
If Matlab is already running and you want to communicate with it in another application (Java), it may be done using a network connection through the localhost. Matlab may listen to some predefined port (for code example see http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example ) and do some action when a "start" trigger is sent via Java (or even some data along with the trigger). In Java you may use the Socket class (some code example may be found here http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html ).
Also it may be done writing data into files. For example, Java adds some command to some file with predefined name (command.txt). Matlab scans this file in a loop and when something is found there it starts calculation (and Java application waits for results in some results.txt file).
I would suggest to start a server in Matlab that listens on a specific port to send/receive data to/from a Java client. By using the eval Matlab command you could even invoke scripts/command/etc. remotely controlled by a Java client.
You might want to have a look at this code example.

Java/python using processBuilder

Good evening all,
am running a python script inside java using processBuilder.
the python script returns a list and i dont know how to get it java and use it since all i can do for the moment with process builder is print errors or outputs.
is it possible to get the list in java as well.
Many thanks
From your scenario, you are looking for inter process communication.
You can achieve this using shared file. Your python script will write the output in text file, and your java program will read the same file.

How do I write to the input stream of an already running java program?

I have a CentOS server which is currently running a java jar application. I need to write a php script to communicate with this running program via its input stream. The java program outputs its output to a log file so I don't need access to the output stream.
I don't want to restart the program, just access the running process and interact with it.
Can someone point me in the right direction?
If portability is not a big matter for you, why not creating your own pipe(s)? I don't know much about the java application but have a look at the "mkfifo" function/command.
First, find the ProcessID of the application. You may do it using:
ps -Af | grep java
Since you are using java, you may feel more convenient with the jps command for finding the PID.
I'll assume PID of the running application is 12345. It suffices to issue the command:
cat >/proc/12345/fd/0
And whatever you type in will be put in the standard input of that application. Note that fd contains the file descriptors used by the application, and I suppose the 0-th file descriptor would always be stdin.
Using PHP for writing into the file (and thus being consumed by the application as input) is possible as well.

Executing interactive shell commands in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Redirect stdin and stdout in Java
I know how to execute shell commands in Java but how to do it so my application can write to its input and read from its output.
I think that you even explained the answer into your question.
When you are running external application from java either using Runtime.exec() or ProcessBuilder you can access standard output and standard input streams. User input stream to read what external application writes and output stream to send commands to external application.
But be careful. Some applications will not get your commands sent from java. For example command ssh in Unix system is designed to avoid its usage by non-human user (e.g. other application). It require to be executed from terminal for security reasons, so you cannot for example run ssh otherhost and then send user/password from java. If you need this you have to run the command via other command line utility named expect that simulates terminal and is driven by script.

Java - communicating with a sub app through the input stream

Is there a way I can start a command-line application from java and then send strings (commands) to its input stream and display its response from its output stream?
I'm using an application with a pretty sophisticated command line interface (vlc). The application has an interpreter that responds to a set of commands. For example, after I start the app, I can start or stop a movie by issuing the command 'pause' on the command line.
I'd like to write a java application that executes the program and issues commands to the program. I've seen many examples of java apps starting an application and getting the output stream of the app displaying the output of the app. But I've never seen an example, in which the java app would send requests to the sub-application.
Is there a way I can do this using java?
Thanks in advance!
So long as the spawned process listens on stdin for input, sure.
You'd launch a Process in the usual way (Runtime.exec()) - I won't document it here, as you say you've seen plenty of examples.
Then once you have a handle to the spawned process, you call the confusingly-named getOutputStream. This gives you an OutputStream, the other end of which is connected to the process' standard input. Hence, any bytes written to this stream can be consumed by your child process, just as if you were typing/piping input from a console.
I will point to a couple of resources that are always worth reading when dealing with Processes; cut-and-paste jobs from arbitrary Google results often don't cover the edge cases properly and can lead to deadlocks:
When Runtime.exec() won't (old, but still relevant)
Five common Java Process pitfalls

Categories

Resources