无法在 Java 终端中 运行 多个命令
Cannot run multiple commands in Java terminal
我不能 运行 使用符号“&&”、“&”、“||”、“;”的多个命令
我检查了几乎所有提出这个问题的问题,但我仍然找不到问题的答案。我可以在 java 命令 shell 中成功 运行 一个命令,但不会超过 1 个。在此先感谢您的帮助!
try {
Process process = Runtime.getRuntime().exec("g++ " + projPath +" -o " + name + " && ./" + name);
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
BufferedReader errinput = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line;
String err;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
while ((err = errinput.readLine()) != null) {
output.append(err + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
acc.setExpandedPane(pane1);
txt.setStyle("-fx-text-fill: black;");
txt.setText(output.toString());
} else {
acc.setExpandedPane(pane1);
txt.setStyle("-fx-text-fill: red;");
txt.setText(output.toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Java 不提供 shell,它只是将提供的字符串拆分为单词并将其作为参数传递给第一个单词提到的可执行文件.
尝试将命令传递给 /bin/bash
(或您选择的另一个 shell):
final String innerCommand = "g++ " + projPath +" -o " + name + " && ./" + name;
final String[] command = {"/bin/bash", "-c", innerCommand};
final Process process = Runtime.getRuntime().exec(command);
我不能 运行 使用符号“&&”、“&”、“||”、“;”的多个命令 我检查了几乎所有提出这个问题的问题,但我仍然找不到问题的答案。我可以在 java 命令 shell 中成功 运行 一个命令,但不会超过 1 个。在此先感谢您的帮助!
try {
Process process = Runtime.getRuntime().exec("g++ " + projPath +" -o " + name + " && ./" + name);
StringBuilder output = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
BufferedReader errinput = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line;
String err;
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
while ((err = errinput.readLine()) != null) {
output.append(err + "\n");
}
int exitVal = process.waitFor();
if (exitVal == 0) {
acc.setExpandedPane(pane1);
txt.setStyle("-fx-text-fill: black;");
txt.setText(output.toString());
} else {
acc.setExpandedPane(pane1);
txt.setStyle("-fx-text-fill: red;");
txt.setText(output.toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Java 不提供 shell,它只是将提供的字符串拆分为单词并将其作为参数传递给第一个单词提到的可执行文件.
尝试将命令传递给 /bin/bash
(或您选择的另一个 shell):
final String innerCommand = "g++ " + projPath +" -o " + name + " && ./" + name;
final String[] command = {"/bin/bash", "-c", innerCommand};
final Process process = Runtime.getRuntime().exec(command);