Not possible to launch a file on a network using Java Desktop?

file-io, java

Solution

It seems that there is a bug when you try to access a resource on a network drive with spaces in the path. See this entry in Sun's bug database.

Since the bug is already a year old, I don't think you'll get a fix anytime soon. Try the latest VM. If that doesn't help, try to get the source for `WDesktopPeer`. Instead of encoding the path, try to keep it as it was (with backslashes and all) and put quotes around it. That might work.

[EDIT] Specifically, don't replace `\` with `/`, do not prepend `file://` and leave the spaces as they are (instead of replacing them with `%20`)

Problem

(I have a problem that I illustrated in this question but had no correct answers. I refined my problem and tried to edit the initial question to reflect that but I guess because of the way SO displays unanswered questions it lost momentum and there is no way to revive it. So I am posting my correct question again). I have a file that resides on a shared network location : ``` "\\KUROSAVVAS-PC\Users\kuroSAVVAS\Desktop\New Folder\Warsaw Panorama.JPG" ``` (The spaces are there intentionally) The following code : ``` import java.awt.Desktop; import java.io.File; import java.io.IOException; public class Test { public static void main(String[] args) { try { String s = "\\\\KUROSAVVAS-PC\\Users\\kuroSAVVAS\\Desktop\\New Folder\\Warsaw Panorama.jpg"; File f = new File(s); System.out.println(f.exists()); Desktop.getDesktop().open(f); } catch (IOException e) { e.printStackTrace(); } } } ``` Prints to the console that the file exists (System.out.println(f.exists());) but throws this exception! : ``` java.io.IOException: Failed to open file:////KUROSAVVAS-PC/Users/kuroSAVVAS/Desktop/New%20%20%20%20%20Folder/Warsaw%20%20%20%20Panorama.jpg. Error message: The system cannot find the file specified. at sun.awt.windows.WDesktopPeer.ShellExecute(WDesktopPeer.java:59) at sun.awt.windows.WDesktopPeer.open(WDesktopPeer.java:36) at java.awt.Desktop.open(Desktop.java:254) at Test.main(Test.java:13) ``` Has anyone any idea why something like this may happen? I have tried everything from creating URIs to decoding them afterwards... Nothing works.

Original source