How to execute /bin/sh with commons-exec?

假如想象 提交于 2019-12-24 03:34:06

问题


This is what I'm doing:

import org.apache.commons.exec.*;
String cmd = "/bin/sh -c \"echo test\"";
new DefaultExecutor().execute(CommandLine.parse(cmd));

This is the output:

/bin/sh: echo test: command not found

What am I doing wrong?


回答1:


This one works for me:-

    CommandLine command = new CommandLine("/bin/sh");
    command.addArguments(new String[] {
            "-c",
            "echo 'test'"
    },false);
    new DefaultExecutor().execute(command);



回答2:


According to the FAQ "It is recommended to use CommandLine.addArgument() instead of CommandLine.parse()".

So try

CommandLine commandLine = new CommandLine("/bin/sh");
commandLine.addArgument("-c");
commandLine.addArgument("echo test", false); // false is important to prevent commons-exec from acting stupid
executor.execute(commandLine);



回答3:


I can reproduce your problem from a shell command line:

# /bin/sh -c '"echo test"'
# /bin/sh: echo test: command not found

I'd say you should try to not quote the command.




回答4:


The command doesn't work because commons-exec doing unnecesary quoting of all arguments, which have space or quote.

This unnecessary quoting of arguments is controlled by handleQuoting flag of each argument. If you create the CommandLine object using it's constructor and addArgument methods, you can set this flag to false.

Like this:

CommandLine commandLine = new CommandLine("/bin/sh");
commandLine.addArgument("-c");
commandLine.addArgument("echo test", false);

(The important part is false as second argument of addArgument method)

And it works! But... it is unconvinient to construct command line manually instead of having it defined in some config file.

CommandLine.parse always sets the handleQuoting flag to true! Who knows why...

I wrote this small helper method using reflection to fix "bad" CommandLine object, created using CommandLine.parse.

public static CommandLine fixCommandLine(CommandLine badCommandLine) {
    try {
        CommandLine fixedCommandLine = new CommandLine(badCommandLine.getExecutable());
        fixedCommandLine.setSubstitutionMap(badCommandLine.getSubstitutionMap());
        Vector<?> arguments = (Vector<?>) FieldUtils.readField(badCommandLine, "arguments", true);
        arguments.stream()
                .map(badArgument -> {
                    try {
                        return (String) FieldUtils.readField(badArgument, "value", true);
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                })
                .forEach(goodArgument -> fixedCommandLine.addArgument(goodArgument, false));
        return fixedCommandLine;
    } catch (Exception e) {
        logger.error("Cannot fix bad command line", e);
        return badCommandLine;
    }
}

It just clones the given CommandLine, setting each argument's handleQuoting flag to false. The method FieldUtils.readField is from commons-lang3 library, but you can use plain reflection if you want.

It allows to parse command line and still successfully execute it.

String cmd = "/bin/sh -c 'echo test'";
new DefaultExecutor().execute(fixCommandLine(CommandLine.parse(cmd)));



回答5:


import org.apache.commons.exec.*; 

CommandLine commandLine = new CommandLine("abc.sh");
commandLine.addArgument("param1"); 
final Executor exec = new DefaultExecutor().execute(commandLine);


来源:https://stackoverflow.com/questions/5080109/how-to-execute-bin-sh-with-commons-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!