Getting insight from this post, I need to have a java program which should run my other program (a C++ program) with some arguments, in Ubuntu, if it is not already running (I need to check if it is already running using ps -A | grep 'test'
to see if the result is null). The command to run my desired program looks like this: ./src/test -w -b -f tracefiles/trace111.txt -z
. How can I do this, no matter what directory my java program is currently in? Unfortunately my java program shows this error: No such file or directory
while if I run the same command from terminal from the same directory of my Java class file, it runs it.
Here is my simple code:
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = "./../../../Downloads/yse.wzt/src/yse4 -w -b -f ../../../Downloads/yse.wzt/tracefiles/capacity.10MbpsUP_1MbpsDown_400RTT_PER_0.0001.txt -z -at=eth0 -an=eth1";
try {
// run the Unix "ps -ef" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec(s);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out
.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
} catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}