tags:

views:

321

answers:

2

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 (if a question has an upvoted answer even if it does not solve your problem, the question no longer gets displayed in the unanswered list) it lost momentum and there is no way to revive it. So i am posting my correct question again. If that breaks the rules please let me know so that i may delete one of them (preferably the other one).

So on with the question. 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.

+3  A: 

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)

Aaron Digulla
:( this is sooo frustrating... What exactly do you mean to not encode the path?
Savvas Dalkitsis
See my edits.
Aaron Digulla
you mean in the WDesktopPeer source?
Savvas Dalkitsis
Yes. Fix the source of WDesktopPeer to leave the path alone and pass it without any modification to the OS native code.
Aaron Digulla
A: 

@Savvas, what else could it be but the WDesktopPeer source?

he could be referring to some part of my code... and i think you should have posted this as a comment to the answer :)
Savvas Dalkitsis