Using the Java exec command if you don't know if spaces exist

I am struggling with a whitespace error in the exec Runtime Java method. Here's what's unique about this problem: the command I'm trying to execute is an input string and may or may not have spaces and not necessarily in any particular format. In any case, I need to execute it. If there are no spaces, I am good; if there are gaps, I'm not so good.

How can I explain both circumstances?

Bonus info at no extra charge: one of the big problems seems to be that I am trying to call the executable in c: \ program files \ blablabla ... and exec seems to break into a space after the 'c: \ program. I am sure that other parameters will also appear in the parameters.

Here is a more specific example of the types of strings I can get. This should fix some confusion:

  • C: \ someApp \ someapp.exe
  • c: \ someApp \ someapp.exe -someParam = foo
  • c: \ program files \ someapp \ someapp.exe
  • c: \ program files \ someapp \ someapp.exe -someParam = bar

The first one works great because it has no spaces. The second one is even normal, because it splits into space and uses the first as a command, and the second as a parameter. The third and fourth examples are divided into the first space, use "C: \ program" and the command "files ..." and (in the case of the fourth line) "-someParam = bar" as parameters.

+5
source share
6 answers

, - , - . , , :


try{
    String[] command = {"cmd", "/c", getMySuperAwesomeString()};
    Runtime.getRuntime().exec(command);
}catch(IOExecption ioe){
    System.err.println("I'm borken");
}

, ProcessBuilder?

+4

: ( J2SE 1.5, Runtime.exec(String []))

, , String , :

String[] args = {"C:\Program Files\app\app.exe","C:\Data Files\data1.dat"};
Runtime.exec(args);

, .

, , .

String input = "c:\\program files\\someapp\\someapp.exe -someParam=bar";
int firstSplit = input.indexOf(".exe") + 4; //account for length of ".exe"
String command = input.substring(0,firstSplit);
String args = input.substring(firstSplit).trim(); //trim off extraneous whitespace
String[] argarray = args.split(" ");
String[] cmdargs = new String[argarray.length + 1];
cmdargs[0] = command;
for (int i = 0; i < argarray.length; i++) {
    cmdargs[i+1] = argarray[i];
}
Runtime.exec(cmdargs);

, ( exe, bat - ). , args, argarray. - ( ) .

+4

JAR :

String qoutation = "\""
String path = "C:\\JAR File\\File.jar"
Runtime.getRuntime().exec("java -jar " + quotation + path + quotation);

: java -jar "C:\Jar File\File.jar"

\ , , . " , / .

+1

, , ?

Runtime.exec(commandstring.split(" "));

( , , , .)

0

, . , :

if("string with possible spaces".contains(" ")) {
    System.out.println("yay");
}

, , . , , , :

String s = ".+\\s.+";
Pattern p = Pattern.compile(s);

Matcher m = p.matcher("string with possible spaces or    tabs");
if(m.matches()) {
    System.out.println("yay");
}

. , , , . , . , . , , , , , . :

  • , -
  • , ( " \")
  • (some input "some input")

, , some "funky" input "some \"funky\" input".

: ('). .

0

, , :

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4506936

http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=8582245ba20fa4da33ac0967e858?bug_id=4491217

, :

JRE, , .

public process exec (String [] cmdarray,                   String [] envp)             IOException

. , .

0

All Articles