添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I would like to pass multiple parameters to a processBuilder and the parameters to be separated by a space.

Here is the command,

String[] command_ary = {dir+"library/crc"," -s ", fileName," ",addressRanges};

I need to provide a space after "fcrc" and after "-p" and in between "filename" and the "addressRange".

Thank you

You don't need to include spaces. The ProcessBuilder will deal with that for you. Just pass in your arguments one by one, without space:

ProcessBuilder pb = new ProcessBuilder(
                         dir + "library/crc",
                         "-s",
                         fileName,
                         addressRanges);
                Ya thats correct but the crc exe which Im using requires space between the crc and -s and between filename and the address ranges passed, otherwise it throws a file not found exception
– mee
                Jun 12, 2013 at 9:52
                @mee are you sure the "file not found" error is not caused by something else?  If you're using a relative path as the fileName then you will presumably need to set the correct working directory for the process (using pb.directory(...) before you call pb.start())
– Ian Roberts
                Jun 12, 2013 at 10:01

We need spaces between arguments in commandline because the commandline need to know which is the first argument, which is the second and so on. However when we use ProcessBuilder, we can pass an array to it, so we do not need to add those spaces to differentiate the arguments. The ProcessBuilder will directly pass the command array to the exec after some checking. For example,

private static final String JAVA_CMD = "java";
private static final String CP = "-cp";
private static final String CLASS_PATH = "../bin";
private static final String PROG = "yr12.m07.b.Test";
private static final String[] CMD_ARRAY = { JAVA_CMD, CP, CLASS_PATH, PROG };
ProcessBuilder processBuilder = new ProcessBuilder(CMD_ARRAY);

The above code will work perfectly.

Moreover, you can use

Runtime.getRuntime().exec("java -cp C:/testt Test");

But it is more convenient to use ProcessBuilder, one reason is that if our argument contains space we need to pass quote in Runtime.getRuntime().exec() like java -cp C:/testt \"argument with space\", but with ProcessBuilder we can get rid of it.

ProcessBuilder processBuilder = new ProcessBuilder("command", "The first argument", "TheSecondWithoutSpace");
                why did I receive down votes, what the heck¿? You haven't used a JSP webshell with 1 parameter it seems, come on, use that CMD_ARRAY, now and tell me, are you going to pass a number of parameters based on the command you want to execute¿? Don't make me laugh ...... these down votes I have received should be the other way round OMG!
– aDoN
                Jan 18, 2018 at 12:09
                This post is several months old dude. Also your code does not compile, that's maybe why your pot was downvoted.
– Dici
                Jan 18, 2018 at 13:10
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.