<p>I've been using the following code to open Office Documents, PDF, etc. on my windows machines using Java and it's working fine, except for some reason when a filename has embedded it within it multiple contiguous spaces like "File[SPACE][SPACE]Test.doc".</p>

<p>How can I make this work?  I'm not averse to canning the whole piece of code... but I'd rather not replace it with a third party library that calls JNI.</p>

<pre><code>public static void openDocument(String path) throws IOException {
    // Make forward slashes backslashes (for windows)
    // Double quote any path segments with spaces in them
    path = path.replace("/", "\\").replaceAll(
            "\\\\([^\\\\\\\\\"]* [^\\\\\\\\\"]*)", "\\\\\\\"$1\"");

    String command = "C:\\Windows\\System32\\cmd.exe /c start " + path + "";

    Runtime.getRuntime().exec(command);            
}
</code></pre>

<p><strong>EDIT:</strong> When I run it with the errant file windows complains about finding the file.  But... when I run the command line directly from the command line it runs just fine.</p>

[{'answer_id': 54967, 'author': 'Dan Dyer', 'author_id': 5171, 'author_profile': 'https://Stackoverflow.com/users/5171', 'pm_score': 4, 'selected': True, 'text': '<p>If you are using Java 6 you can just use the <a href="http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html#open(java.io.File)" rel="nofollow noreferrer">open method of java.awt.Desktop</a> to launch the file using the default application for the current platform.</p>\n'}, {'answer_id': 55018, 'author': 'Matt Cummings', 'author_id': 828, 'author_profile': 'https://Stackoverflow.com/users/828', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure if this will help you much... I use java 1.5+\'s <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html" rel="nofollow noreferrer">ProcessBuilder</a>  to launch external shell scripts in a java program.  Basically I do the following: ( although this may not apply because you don\'t want to capture the commands output; you actually wanna fire up the document - but, maybe this will spark something that you can use )</p>\n\n<pre><code>List&lt;String&gt; command = new ArrayList&lt;String&gt;();\ncommand.add(someExecutable);\ncommand.add(someArguemnt0);\ncommand.add(someArgument1);\ncommand.add(someArgument2);\nProcessBuilder builder = new ProcessBuilder(command);\ntry {\nfinal Process process = builder.start();\n...    \n} catch (IOException ioe) {}\n</code></pre>\n'}, {'answer_id': 55059, 'author': 'KeithL', 'author_id': 5478, 'author_profile': 'https://Stackoverflow.com/users/5478', 'pm_score': 0, 'selected': False, 'text': '<p>The issue may be the "start" command you are using, rather than your file name parsing. For example, this seems to work well on my WinXP machine (using JDK 1.5)</p>\n\n<pre><code>import java.io.IOException;\nimport java.io.File;\n\npublic class test {\n\n    public static void openDocument(String path) throws IOException {\n        path = "\\"" + path + "\\"";\n        File f = new File( path );\n        String command = "C:\\\\Windows\\\\System32\\\\cmd.exe /c " + f.getPath() + "";\n            Runtime.getRuntime().exec(command);          \n    }\n\n    public static void main( String[] argv ) {\n        test thisApp = new test();\n        try {\n            thisApp.openDocument( "c:\\\\so\\\\My Doc.doc");\n        }\n        catch( IOException e ) {\n            e.printStackTrace();\n        }\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/54861', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2443/']