Java Runtime.getRuntime().exec(…) will and will not work…

The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class’s exec() method.

There are four overloaded versions of the exec() command:

* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);

For each of these methods, a command — and possibly a set of arguments — is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.

Let me give an example:

Runtime r=Runtime.getRuntime();
Process p=null;

try {
String notepad = “c:\\windows\\notepad.exe”;
String wordpad = “c:\\windows\\regedit.exe”;

p = r.exec(notepad);
p = r.exec(wordpad);

}catch(Exception e) {
System.out.println(“error===”+e.getMessage());
e.printStackTrace();
}

The example given above clearly shows how to invoke executable through the runtime. If you look carefully the exec() method is responsible for creating a new process for the executable, the parameter of which is a command or rather a location to the executable in this case. Look at the command carefully. Lets take one of the commands as example:

“c:\\windows\\regedit.exe”

If you notice the backslashes are escaped using yet another backslash. What if there was a space in the command? For example, “c:\\Program Files\\someFolder\\some.exe”. If you try to print the string then the out would to exactly, c:\Program Files\someFolder\some.exe with the backslashes properly escaped and the space exactly where it should be. But the fact of the matter is that it wont be executed by the exec() method. An exception will be thrown suggesting that “c:\Program” cannot be found. Everything beyond the space has been omitted by the method. Therefore the spaces are not escaped by the exec() method which is bug posted in the Sun Developer Network (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468220). There happens to be a workaround. Instead of assigning the command to a string object, if it was assigned to a string array, the exec() properly executes the command.

Advertisements

19 thoughts on “Java Runtime.getRuntime().exec(…) will and will not work…

  1. This is something I couldn’t never really get a nack for. “It worked two seconds ago, what the hell?! Do you hate me?” Then you find you accidentally put a space in there some where. Finally said that this human to computer taming was not for me.

    -Fluff.

  2. Try “\”C:\\Program Files\\Internet Explorer\\iexplore.exe\””
    That is add double quotes at both ends and escape that. However, in Windows Vista and JDK 6 Update 11, I did not face the problem that you mentioned.

  3. Here my problem was something else, some exe in vista requires elevated priviledge like regedit.exe. I have to learn how to handle this in Java. What I get is:
    error===Cannot run program “regedit.exe”: CreateProcess error=740, The requested operation requires elevation
    java.io.IOException: Cannot run program “regedit.exe”: CreateProcess error=740, The requested operation requires elevation

  4. How did you manage to handle that problem? How did make that process have elevated privilege in order to run it?

    • To run a program with elevated privilieges use:

      ProcessBuilder builder = new ProcessBuilder(new String[] { “cmd.exe”, “/C”, “ProgramWithElevatedPrivs.exe”});
      Process newProcess = builder.start();

      Note that this is preferred to Runtime.exec, although it should work well with the same argument (ie. the string array)

    • What does the UI of chrome actually say? When you try to register for the RSS feed what actually happens? Is the problem limited to only chrome or does it happen in all the other browsers?

  5. Unable to launch the java TM update installer: The requested operation requtres elevation. What does this mean ?

    • Your machine is trying to update the jre version but could not. It can be due to various reason but I think the only way you can update the jre version is from the sun website itself. Do not try to uninstall it, the installer from the sun website would do that for you.

  6. Matin Saad Abdullah sir gave an excellent solution. That is to call the program via another program like a shell script in Linux or “runas” in Windows which will do the requested privilege elevation.

  7. “C:\\Program Files” executing “C:\\Program”, is NOT a bug its by Design …. If you open a regular terminal and type C:\\Program Files\someFolder\some.exe it will give you the same error …

    The proper syntax for the terminal is “C:\Program Files\someFolder\some.exe”, Notice the Quotes, because in a single line terminal command you need to be able to pass arguments
    “C:\Program Files\someFolder\some.exe” -execute “more quotes”

    so in Java we would type this into java single line execute function as designed as

    p = r.exec(“\”c:\\Program Files\\someFolder\\some.exe\” -execute \”some spaced text\””);

    here we simply escape the quotes

  8. I have run my IDE as administrator and now i have the privilege to do anything 😀

    BUT after right clicking on executable.jar I have not get any “Run as administrator” so i have made a wrapper.bat file and run it as admin.

    Thankyou!

  9. Pingback: Get Free Memory on Unix System in Java « Missing Link

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s