6

(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.

6
  • And does the same file path (i.e. with spaces) work when the file is local to your computer (i.e. on C:)? Is the problem related to the network file system? Commented Sep 1, 2009 at 15:14
  • If i paste this "\\\\KUROSAVVAS-PC\\Users\\kuroSAVVAS\\Desktop\\New Folder\Warsaw Panorama.jpg" into the "run" dialog the file opens normally. In this example the share also resides on my local disk but in general it should work with any network location. Commented Sep 1, 2009 at 15:17
  • (the spaces were automatically removed on my previous comment. they are there...) I also wanted to say that i am authenticated against the share Commented Sep 1, 2009 at 15:18
  • Since you asked, creating duplicate questions isn't necessarily against the rules, but it will most likely get the newer question closed on you. Commented Sep 1, 2009 at 15:24
  • i should vote my old one closed...this seems more helpfull Commented Sep 1, 2009 at 15:29

4 Answers 4

9

With java 7 you can do this

public static void main(String[] args) throws IOException {
    String s = "\\\\KUROSAVVAS-PC\\Users\\kuroSAVVAS\\Desktop\\New     Folder\\Warsaw    Panorama.jpg";
    Path p = Paths.get(s);
    Desktop.getDesktop().browse(p.toUri());
}
Sign up to request clarification or add additional context in comments.

Comments

8

Java 6 solution:

public static void launchFile(File file) {
    if (!Desktop.isDesktopSupported())
        return;
    Desktop dt = Desktop.getDesktop();
    try {
        dt.open(file);
    } catch (IOException ex) {
        // this is sometimes necessary with files on other servers ie
        // \\xxx\xxx.xls
        launchFile(file.getPath());
    }
}

// this can launch both local and remote files
public static void launchFile(String filePath) {
    if (filePath == null || filePath.trim().length() == 0)
        return;
    if (!Desktop.isDesktopSupported())
        return;
    Desktop dt = Desktop.getDesktop();
    try {
        dt.browse(getFileURI(filePath));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

// generate uri according to the filePath
private static URI getFileURI(String filePath) {
    URI uri = null;
    filePath = filePath.trim();
    if (filePath.indexOf("http") == 0 || filePath.indexOf("\\") == 0) {
        if (filePath.indexOf("\\") == 0){
            filePath = "file:" + filePath;
            filePath = filePath.replaceAll("#", "%23");
        }
        try {
            filePath = filePath.replaceAll(" ", "%20");
            URL url = new URL(filePath);
            uri = url.toURI();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        } 
    } else {
        File file = new File(filePath);
        uri = file.toURI();
    }
    return uri;
}

This answer was on the bug report, but I've edited it to fix when there is a hash.

Comments

7

TL;DR of ZAMMBI's answer (+1 BTW). (Using Java 6)

This works, as expected

Desktop.getDesktop().open(new File("\\\\host\\path_without\\spaces.txt"));  //works

This fails, due to a known Java bug:

Desktop.getDesktop().open(new File("\\\\host\\path with\\spaces.txt"));    //fails <shakes fist>

This work-around works

Desktop.getDesktop().browse(new URI("file://host/path%20with/spaces.txt"))  //works (note slash direction and escape sequences)

This work-around seems like it should work, but does not:

Desktop.getDesktop().browse((new File("\\\\host\\path with\\spaces.txt")).toURI());

This work-around works, and seems to be the most general form:

File curFile = new File("\\\\host\\path with\\or_without\\spaces\\local or network.txt");
Desktop.getDesktop().browse(new URI(curFile .toURI().toString().replace("file:////","file://")));

Comments

4

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)

2 Comments

you mean in the WDesktopPeer source?
Yes. Fix the source of WDesktopPeer to leave the path alone and pass it without any modification to the OS native code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.