Java程序在Ubuntu中运行我的另一个程序 从洞察中,我需要一个java程序,它应该运行我的另一个程序(C++程序),在Ubuntu中,如果它没有运行(我需要检查它是否已经运行了,使用 ps -a GRIP测试< 查看结果是否为空)。运行所需程序的命令如下:/src/test-w-b-f tracefiles/trace111.txt-z。无论我的java程序当前在哪个目录中,我如何做到这一点?不幸的是,我的java程序显示了这个错误:没有这样的文件或目录,而如果我从终端从java类文件的同一目录运行相同的命令,它就会运行它
以下是我的简单代码:Java程序在Ubuntu中运行我的另一个程序 从洞察中,我需要一个java程序,它应该运行我的另一个程序(C++程序),在Ubuntu中,如果它没有运行(我需要检查它是否已经运行了,使用 ps -a GRIP测试< 查看结果是否为空)。运行所需程序的命令如下:/src/test-w-b-f tracefiles/trace111.txt-z。无论我的java程序当前在哪个目录中,我如何做到这一点?不幸的是,我的java程序显示了这个错误:没有这样的文件或目录,而如果我从终端从java类文件的同一目录运行相同的命令,它就会运行它,java,ubuntu,Java,Ubuntu,以下是我的简单代码: 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
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);
}
}
}
您需要将javabin方向添加到PATH变量中。编辑主目录中的.bashrc并添加以下行:
export PATH=/path/to/java/bin:$PATH
您需要“源”您的.bashrc文件或重新启动终端,路径才能生效。在主目录中键入:
source .bashrc
有关在Ubuntu上设置环境变量的更多信息,请访问:
您可以使用
echo$PATH
确认路径分配是否正确,或者只需从java/bin目录以外的目录键入javac
。此答案与实际问题无关。问题是java不在路径上,因此程序失败。这怎么不相关呢?其实这些都是基础!已经完成了。发布一个你是如何解决的。@LukePeterson好吧,Java不在路径上不是问题……问题是我的Java程序找不到/运行其他程序。