运行 命令包含使用 apache commons exec 的多个参数时出错
Error while running command consist of multiple parameters using apache commons exec
我在使用 apache commons exec 运行 执行以下命令时遇到问题:sysctl -n net.ipv4.neigh.default.gc_thresh3
问题是出现以下错误:
Exception in thread "main" java.lang.RuntimeException: Could not run command [/bin/sh, -c, sysctl, -n, net.ipv4.neigh.default.gc_thresh3]
at Testing.runCommand(Testing.java:44)
at Testing.main(Testing.java:97)
Caused by: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
at Testing.runCommand(Testing.java:31)
... 1 more
下面是我的代码:
private String runCommand(String cmd, String params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);
try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());
if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}
} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}
我正在使用命令 - /bin/sh
和参数 - -c sysctl -n net.ipv4.neigh.default.gc_thresh3
。
我如何使用 apache commons exec 运行 命令而不遇到此类错误?
我不需要完整的解决方案。任何好的方向我都可以。
问题是 /bin/sh
不是 运行 这种命令的正确路径。该命令应在以下位置执行:/sbin/sysctl
.
此外,不同的操作系统需要不同的 运行 路径。见 man sysctl
所以,cmd 是 - /sbin/sysctl
和 params - new String[] { "-n", "net.ipv4.neigh.default.gc_thresh3" }
代码将如下所示:
private String runCommand(String cmd, String[] params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params, false);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);
try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());
if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}
} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}
我在使用 apache commons exec 运行 执行以下命令时遇到问题:sysctl -n net.ipv4.neigh.default.gc_thresh3
问题是出现以下错误:
Exception in thread "main" java.lang.RuntimeException: Could not run command [/bin/sh, -c, sysctl, -n, net.ipv4.neigh.default.gc_thresh3]
at Testing.runCommand(Testing.java:44)
at Testing.main(Testing.java:97)
Caused by: org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:153)
at Testing.runCommand(Testing.java:31)
... 1 more
下面是我的代码:
private String runCommand(String cmd, String params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);
try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());
if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}
} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}
我正在使用命令 - /bin/sh
和参数 - -c sysctl -n net.ipv4.neigh.default.gc_thresh3
。
我如何使用 apache commons exec 运行 命令而不遇到此类错误?
我不需要完整的解决方案。任何好的方向我都可以。
问题是 /bin/sh
不是 运行 这种命令的正确路径。该命令应在以下位置执行:/sbin/sysctl
.
此外,不同的操作系统需要不同的 运行 路径。见 man sysctl
所以,cmd 是 - /sbin/sysctl
和 params - new String[] { "-n", "net.ipv4.neigh.default.gc_thresh3" }
代码将如下所示:
private String runCommand(String cmd, String[] params) {
CommandLine commandLine = new CommandLine(cmd);
commandLine.addArguments(params, false);
ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout, stderr);
ExecuteWatchdog watchdog = new ExecuteWatchdog(30000); // 30s timeout
DefaultExecutor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setWatchdog(watchdog);
try {
int retCode = executor.execute(commandLine);
System.out.println("Executed '" + cmd + "'\n"
+ "returnCode: " + retCode + "\n"
+ "stdout:\n" + stdout.toString() + "\n"
+ "stderr:\n" + stderr.toString());
if (retCode == 0) {
return stdout.toString();
} else {
throw new NonZeroExitStatusReturnedException(commandLine.toString(), retCode);
}
} catch (IOException e) {
throw new RuntimeException("Could not run command "+ commandLine.toString(), e);
}
}