Trouble in calling system commands from Java - java

i have this problem:
I have created a bash script that performs some tasks. At one point this script call a java program. This java program perform some wsdl tasks and in one point have to call an external fortran program that perform a simulation and put the outcomes inside a file "oucomes.dat". program.exe is perfectly executed but the java program seems unable to open the file created by the fortran program. The code in the java program that call the fortran simulation is:
Runtime.getRuntime().exec("./script.sh");
where script.sh contains
#!/bin/bash
./program.exe
When i call first program.exe and then the java program the java program can read prefectly "outcomes.dat". The problem is that i have to call program.exe from inside of java because i need some data in realtime from a wsdl service and eventually send data back to the wsdl service. So i guess that the problem is in the form that i call program.exe from inside the java. One solution may be to split the code in java in two program and putting between the to program a call to program.exe. But i would like a faster solution (in terms of CPU and memory usage). Which is the correct form to call the program.exe in order to allow the java program to read the "outcome.dat"?
PS: i use linux.

It doesn't look as though you are waiting for script.sh to finish. You need to do something like this (untested):
Process p = Runtime.getRuntime().exec("./script.sh");
p.waitFor();
You can also test the process's exit value using exitValue().
See http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html .

Related

Bypassing event watcher in java program with batch file

I'm currently trying to automate some data export from a monitoring program written in JAVA. I don't have the source code so I got a JAVA decompiler to look inside my .jar file.
I found the functions that I want to execute but I don't manage to execute them by commands in cmd prompt.
When I launch the .jar file, the main() function starts, it launches two event watcher threads that wait for actions. The actions that I want to perform are normally accessed trough buttons pressing.
I tried launching hese functions directly but my batch lines aren't even read. The only read line is the first one, launching the program, then nothing, it just waits and doesn't read the other lines.
I'm a bit lost and I could sure use some help !
Thanks in advance

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.

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.

Executing C program object code using java in linux

I was developing a simple C code generator in java in Linux and i wanted my java program to automatically compile and run the generated C code,i.e. the .out file. Though i have been able to compile it successfully I am not able to run the compiled object code. Can anyone please write the code to suggest how to execute the C code using the java program.
If you're successfully creating the .out file, then you should be able to run it with one of the Runtime#exec functions:
Runtime.getRuntime().exec("./a.out");
...or (more control) via the stuff in the Process class. The Process class stuff lets you do things like control the input and output (via streams).

Categories

Resources