Question

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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top