0

Issue:- If the executable command contain's any spaces then System.exec is omitting the string content after the first space.

For example:- if command="/opt/GUIInstaller/installers/abc def gh.bin" Then java is executing command up-to /opt/GUIInstaller/installers/abc only and resulting a error like java.io.IOException: "/opt/GUIInstaller/installers/abc": error=2, No such file or directory

protected void launch(final String command) 
{
    try 
    {
        if(command.contains("null"))
        {
            logger.error("Installer is not located in the specified folder: "+command);
            System.exit(0);
        }
        runTime.exec(command);
    }
    catch (IOException ioException) {
        logger.error(ioException.getMessage(), ioException);
    }
}

Is I am doing any mistake, please help me to solve this issue.

Environment:- Java7 update9 + RHEL6

1
  • 1
    Try "/opt/GUIInstaller/installers/abc\\ def\\ gh.bin" to escape the spaces Commented May 29, 2013 at 11:13

2 Answers 2

3

As described in the javadocs of Process#exec(), exec(String) simply splits the given command string into tokens via StringTokenizer. If you do that job jourself by passing tokens to exec(), spaces in there are no problem:

runTime.exec(new String[] {"/opt/GUIInstaller/installers/abc def gh.bin", "--param1=foo"});
Sign up to request clarification or add additional context in comments.

3 Comments

Even the solution is working, installer is not launched(I mean GUI window not appeared), if I tried it manually I am able to launch the installer window, even I check the process output its not giving any error. Please help me to solve this issue.
At-least let me know --param1=foo argument what it will do
Oh. This is just some random commandline parameter to illustrate how you'd specify those. If your command doesn't take any parameters, you don't need that.
0

Add

if(command.contains(" ")){command.replace(" ","\\ ");}

before the runTime.exec(command);

This basically just replaces spaces with escaped spaces..

Edit: Or to make it smoother try to execute this

runTime.exec(command.replace(" ","\\ "));

without adding the aforementioned line..

4 Comments

You could probably do away with the if statement there, if there's no space the replace will silently do nothing.
Yes, I thought of that after posting. That's why I edited it. Thanks for your notice though ;)
"\ " is not an acceptable Java string literal. If you need a backslash in a string, you must escape it.
@kgopi Listen to creinig's advice, that's the correct one. Don't let Runtime.exec parse your command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.