11

I'm getting an error :

IllegalArgumentException : Executable name has embedded quote, 
split the arguments 

While running the

Runtime.getRuntime().exec(cmd, envTokens, file1);

I'm using Windows7 and Java7 machine .

Same line of code is working fine for other environments .

Suggest me some way .

Gaurav Singh
  • 12,707
  • 5
  • 22
  • 24
  • 1
    Show how your parameters `cmd`, `envTokens`, `file1` look like. Do they contain quotes? – mvp Jun 03 '13 at 05:26
  • my parameters doesn't contains quotes ... but they contains spaces between them and i have specified earlier that same line of code in working fine in other environments – Gaurav Singh Jun 03 '13 at 05:52
  • you might be able to overcome this by protecting spaces inside by extra quotes or backslashes, but I think [this answer below](http://stackoverflow.com/a/16890476/1734130) appears to address your issue, especially if you are running most recent JDK – mvp Jun 03 '13 at 05:56

2 Answers2

15

This happens because of a change in Java 7 update 21/Java 6 update 45.

The solution to your problem is to refactor your code to use java.lang.ProcessBuilder instead. For instance:

ProcessBuilder pb = new ProcessBuilder("command", "argument1", "argument2");
Map<String, String> env = pb.environment();
env.put("var1", "value1");
Process p = pb.start();
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
  • can u suggest me some way to do it from Runtime.getRuntime().exec() only , if possible? – Gaurav Singh Jun 03 '13 at 06:20
  • 1
    I guess splitting the cmd yourself and passing the array to exec (instead of the string with spaces) will still work. – Aleksander Blomskøld Jun 03 '13 at 06:27
  • i am getting the same error message with my source code in the blackberry compiler in eclipse. It says cant start compiler. then the message. What do i do? Where to look.? – ejobity Jun 11 '13 at 19:31
  • 3
    Note that the same change was implemented in Java 6 update 45. http://www.oracle.com/technetwork/java/javase/6u45-relnotes-1932876.html – Maximus Jul 17 '13 at 15:17
1

You have to put your parameters in a String array:

    String a = quote(exeFullPath);        
    String b = paramB;
    String[] cmd = new String[]{a,b};
    Process myExec = Runtime.getRuntime().exec(cmd, null, parentFolder);
Alex Byrth
  • 1,328
  • 18
  • 23