Java 使用apache commons exec运行包含多个参数的命令时出错

Java 使用apache commons exec运行包含多个参数的命令时出错,java,sysctl,apache-commons-exec,Java,Sysctl,Apache Commons Exec,使用apache commons exec运行以下命令时遇到问题:sysctl-n net.ipv4.neigh.default.gc\u 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.runCom

使用apache commons exec运行以下命令时遇到问题:
sysctl-n net.ipv4.neigh.default.gc\u 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);
        }
}
我正在使用cmd-
/bin/sh
和params-
-c sysctl-n net.ipv4.neigh.default.gc\u thresh3

如何使用apache commons exec运行这样的命令而不遇到此类错误?

我不需要完整的解决方案。任何好的方向对我都没问题。

问题是
/bin/sh
不是运行此类命令的正确路径。该命令应在以下位置执行:
/sbin/sysctl

另外,不同的操作系统需要不同的运行路径。请参见
man系统控制列表

因此,cmd是-
/sbin/sysctl
和params-
新字符串[]{“-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);
        }
}