从this post获取洞察力,我需要有一个java程序,它应该在Ubuntu中运行我的其他程序(一个带有一些参数的程序),如果它还没有运行(我需要检查一下)已经使用ps -A | grep 'test'
运行以查看结果是否为null。运行我想要的程序的命令如下所示:./src/test -w -b -f tracefiles/trace111.txt -z
。无论我的java程序目前在哪个目录中,我该怎么做?不幸的是我的java程序显示了这个错误:No such file or directory
而如果我从终端从我的Java类文件的同一目录运行相同的命令,它会运行它。
这是我的简单代码:
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);
}
}
}
答案 0 :(得分:0)
您需要将JAVA bin方向添加到PATH变量中。编辑主目录中的.bashrc并添加以下行:
export PATH=/path/to/java/bin:$PATH
您需要“获取”.bashrc文件或重新启动终端才能使路径生效。在您的主目录中键入:
source .bashrc
有关在Ubuntu上设置环境变量的更多信息,请访问:https://help.ubuntu.com/community/EnvironmentVariables#Persistent_environment_variables。
您可以使用echo $PATH
确认PATH分配是否正确,或者只是从java / bin目录以外的目录中键入javac
。