【问题标题】:Error while running command consist of multiple parameters using apache commons exec运行命令时出错,包含使用 apache commons exec 的多个参数
【发布时间】:2023-03-11 07:44:01
【问题描述】:

我在使用 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);
        }
}

我正在使用 cmd - /bin/sh 和 params - -c sysctl -n net.ipv4.neigh.default.gc_thresh3

如何使用 apache commons exec 运行这样的命令而不会遇到此类错误?
我不需要完整的解决方案。任何好的方向都适合我。

【问题讨论】:

    标签: java sysctl apache-commons-exec


    【解决方案1】:

    问题是/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);
            }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2017-02-05
      • 2018-05-15
      相关资源
      最近更新 更多