Executing external Java program from a webapp - java

I am trying to call an external Java class file from a servlet running on Tomcat 6, Windows 7, 64bit .
There are already some threads on this subject around, but none are really helping me.
Fyi, I have successfully been able to do this if I run it from the shell directly.
Im using a ProcessBuilder to execute the command like this
ProcessBuilder bp = new ProcessBuilder("cmd.exe","/C","java", "TheExternalClass", "ParameterA" });
I'm also consuming the errorStream and inputStream from the created Process.
When I run it from the servlet it simply stalls.
If I for example swith the java command to dir it does work as expected, leading me to believe it has something to do with either memory, or issues starting up a new Java Process from Tomcat or something like that.
Anybody has a pointer or a good idea on how to solve this?
Some other posts on the topic:
http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1
call a java program from a webapp in tomcat server - the java program is out side of tomcat server
Tomcat fails to execute external java program
Thanks much for reading.

The code above works and it doesn't stall your servlet. My guess is that you call p.waitFor() or similar later or that you read the output streams of the process in the JSP thread - and that will block.
If you don't want to block, you have two options:
Use AJAX to poll the JSP in the background. The JSP will still block but the browser will be usable.
Create a background thread that reads the output streams. That will make the JSP return immediately but you will have to find a way to send the process results to the browser because it won't know what happens on the server.

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.

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.

Output of a Java class run through a Python script, that is in turn executed by an Apache web server

I'm having a problem runnig Java class run through a Python script, that is in turn executed by an Apache web server.
I have the following file, accesible via an Apache webserver
script.cgi
#!/usr/bin/python
os.system("java HelloWorld")
sys.stdout.flush()
The I run the script from the shell, it runs properly. However, when I access it via a web browser, the os.system("java ...") returns exit status 1536.
Any idea why this is happening?
This is running on Linux Mint 13. Please let me know what extra information I can provide.
Thank you
When running the script from your command line you probably have different enviroment variables set and you have a different security content.
Make sure that your webserver finds the files (E.g. try using full path names) and check if everyone has the execute permission for java and read to the folder and file(For security reasons I'm not sure if that is a good idea tough).

Running R script from Java

I have a R script I need to call from Java and run. I tried this code: Runtime.getRuntime().exec("Rscript pathTo/R/myScript.R"). I run it from windows command it worked fine, but when I run java class with this code in Eclipse, nothing happens. The console doesnt show anything no error no logs. Can someone tell me how to run this script from Java?
By default, a Process launched from java has its standard input, standard output and standard error redirected to pipes, which you can access from within java. Unless you read from the standard output and error pipes and transfer the text to the output of the Java application yourself, no output will become visible. Furthermore, if the internal buffer of the pipe gets full, then the child application might even block while waiting for root to write its data. So the process probably will hang and never terminate.
Since Java 7, you can have the child process inherit its I/O channels from your Java application using ProcessBuilder.inheritIO. That saves you all the trouble to read from those streams yourself.

Permission error while trying to access an (server) program started by a Java program

I am starting a server application (normally to be started from the Unix command line) by using Runtime.getRuntime().exec("path/mmserver"). My problem is now that as long as my Java program, which started that server runs, the server is correctly accessible (from command line and other programs). But when my Java program exits the sever is not accessible anymore (the process of the server is still running). I just get such a error message when trying to access the server: "Error: permission_error(flush_output(user_output),write,stream,user_output,errno(32))".
The server is a blackbox for me.
I am just looking for other ways to start a new process. And maybe someone has a hint why I get that permission error (even if one doesn't know what that server exactly is ... you rather won't know it).
I'm guessing your server program is trying to write to standard output or perhaps standard error (System.out / System.err in Java terms) which it implicitly inherited from your Java program but which turn into pumpkins when your Java program goes away.
A simple solution might be for your Java program to exec a shell script which starts your server as a background process (using START (Windows) or & (Unix)) with explicitly redirected I/O streams.
The Java library has recently gotten some nice updates to the Process class (I think) that allow you to do a lot of fiddling with the streams, but I don't have much experience there so I can't offer a detailed suggestion.
EDIT: My suggestion from the middle paragraph. Untested, sorry!
File server-runner.sh:
#!/bin/bash
/path/mmserver >/dev/null &
You'll need to chmod +x server-runner.sh, of course.
Then, from your Java program, you exec the script server-runner.sh rather than your mmserver.
If you want to kill mmserver, you'll have to find it in ps -ux and use kill on the process number.

Categories

Resources