JAVA执行Linux命令报错Cannot run program error=2 , No such file or directory

JAVA执行Linux命令报错Cannot run program error=2 , No such file or directory

IP属地: 河北
2019.04.22 14:00:00字数 411阅读 6,648

JAVA使用ProcessBuilder运行Linux命令报错: start failed:Cannot run program "xxx" (in directory "xx"): error=2, No such file or directory。

网上找了各种资料都没解决,最后想起azkaban的源码里也是用的ProcessBuilder来执行shell命令,于是翻了一下代码,找到了解决方案,把azkaban里的partitionCommandLine这个方法对command做一下处理,就ok了。

代码如下:

run_command(partitionCommandLine(command), work_path);

/**

*执行命令

*/

public boolean run_command(final String[] command, final File work_path) throws IOException, InterruptedException {

        log.info("COMMAND:" + command);

        List<String> result_list = new ArrayList<>();

        ProcessBuilder hiveProcessBuilder = new ProcessBuilder(command);

        hiveProcessBuilder.directory(work_path);

        hiveProcessBuilder.redirectErrorStream(true);

        Process hiveProcess = hiveProcessBuilder.start();

        BufferedReader std_input = new BufferedReader(new InputStreamReader(hiveProcess.getInputStream(), "UTF-8"));

        BufferedReader std_error = new BufferedReader(new InputStreamReader(hiveProcess.getErrorStream(), "UTF-8"));

        String line;

        while ((line = std_input.readLine()) != null) {

            result_list.add(line);

        }

        while ((line = std_error.readLine()) != null) {

            log.error(line);

        }

        hiveProcess.waitFor();

        if (hiveProcess.exitValue() != 0) {

            log.error("failed to execute:" + command);

            return false;

        }

        log.info("execute success:" + command);

        IOUtils.closeQuietly(std_input);

        IOUtils.closeQuietly(std_error);

        return true;

    }

/**

*对命令进行处理

*/

public static String[] partitionCommandLine(final String command) {

        final ArrayList<String> commands = new ArrayList<>();

        int index = 0;

        StringBuffer buffer = new StringBuffer(command.length());

        boolean isApos = false;

        boolean isQuote = false;

        while (index < command.length()) {

            final char c = command.charAt(index);

            switch (c) {

                case ' ':

                    if (!isQuote && !isApos) {

                        final String arg = buffer.toString();

                        buffer = new StringBuffer(command.length() - index);

                        if (arg.length() > 0) {

                            commands.add(arg);

                        }

                    } else {

                        buffer.append(c);

                    }

                    break;

                case '\'':

                    if (!isQuote) {

                        isApos = !isApos;

                    } else {

                        buffer.append(c);

                    }

                    break;

                case '"':

                    if (!isApos) {

                        isQuote = !isQuote;

                    } else {

                        buffer.append(c);

                    }

                    break;

                default:

                    buffer.append(c);

            }

            index++;

        }

        if (buffer.length() > 0) {

            final String arg = buffer.toString();

            commands.add(arg);

        }

        return commands.toArray(new String[commands.size()]);

    }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12199764/viewspace-2150340/,如需转载,请注明出处,否则将追究法律责任
posted @ 2023-08-04 21:48  商君治国安邦之张莽  阅读(1630)  评论(0编辑  收藏  举报