0

I stumbled upon something really weird in the code down below. I want to open a *.pdf file in Java, using the Runtime.getRuntime().exec("some command").waitFor(); statement.

public static void main(String[] args) {
    File file = new File("C:\\test123.pdf");

    try {
        Runtime.getRuntime()
               .exec("rundll32 url.dll,FileProtocolHandler " + file.toString())
               .waitFor();
    }
    catch (IOException e) {
        // do something
    }
    catch (InterruptedException e) {
        // do something
    }
}

Now when the path contains single spaces, like

new File("C:\this is a test.pdf)
it works fine, but if I add a second space on some place, like
new File("C:\two  spaces")
it does not work, but it also does not give a warning or error at runtime. It just doesn't do anything...

First of all, can somebody explain why this not works? And second, is it possible to fix this, and if so how (but not a solution like ''remove the second space'')?

Thanks a lot for helping me out!

RvanHeest
  • 869
  • 6
  • 12
  • Maybe wrap the path to the file in single quotes? Also it probably does do something... Try using a Process and ProcessInfo object, redirect the STDOut and Err streams to your console. Or alternately in windows use the >> operator followed by a path to a text file as the last part of the string in the .exe call to direct the output of the process there. I'm willing to bet that the second space makes the shell think everything after the first space is a switch to the command, and its throwing an error. – Mark W Jan 23 '14 at 22:14

1 Answers1

1

In Windows, file names containing special characters are stored in a special format for compatibility with older systems that understand only format FAT and FAT16 where file names with the size of 8.3 - 8 characters for the file name + 3-character extension.

For this in table FAT32 uses a standard FAT table for short names (ie names in 8.3 format) with a special format recording system indicating that the file also has a long name.

Thus, working with files in a terminal emulator Windows, you can refer to files using the name as long and short. Long name if it contains a space must be enclosed in double quotes.

See also

Community
  • 1
  • 1
  • Thanks for your answer. The question you pointed to helped me a lot! Apparently I had to use `Runtime.getRuntime().exec("cmd /c start \"\" \"" + file.getAbsolutePath() + "\"".split(" "));`. I have absolutely no clue what this command does, but it works! – RvanHeest Jan 24 '14 at 08:45