8

From my application written in java I want to open a folder, using the operating system file explorer.

I use Desktop.open(new File(path))

This works fine on windows, but on ubuntu 11.10 (linux) it doesn't work. Using the Desktop.open to open a file does work, both on ubuntu and windows.

Using a step in between: File fPath=new File(fPath) and testing it with fPath.exists() and fPath.isDirectory() both gives true.

using the Desktop.open(new File(path)) gives me this exception:

java.io.IOException: Failed to show URI:file:/and/here/the/path/I/use/
at sun.awt.X11.XDesktopPeer.launch(Unknown Source)
at sun.awt.X11.XDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)

I was not able to test this on an apple computer yet, but I hoped the Desktop.open(new File(path)) was system independent.....

by the way, the complete code:

    Desktop desktop = null;
    // Before more Desktop API is used, first check
    // whether the API is supported by this particular
    // virtual machine (VM) on this particular host.
    if (!Desktop.isDesktopSupported()) {
        // show Error
        return;
    }
    desktop = Desktop.getDesktop();
    String path = "here the path ";
    // by the way: I use System.getProperty("file.separator") as file seperator
    try {
        File fPath=new File(path);
        if(!fPath.exists()){
            // show Error
            return;

        }
        if(!fPath.isDirectory()){
            // show Error
            return;

        }
        desktop.open(new File(path));
    } catch (IOException e) {
        log.severe(e.getMessage());
        e.printStackTrace();
        // show Error
        return;
    }

Some extra information: OS: Linux (3.0.0-16-generic - amd64)

Java: 1.6.0_30-b12

Java home: /opt/java/64/jre1.6.0_30

3
  • by the way, in the end the code can be a lot more compact, I used several more lines to build in some extra checks to debug this.... Desktop.getDesktop().open(new File(path)); should do the trick in 1 line... the try catch around it, won't hurt. As well the Desktop.isDesktopSupported() catch. Commented Mar 13, 2012 at 21:27
  • Still didn't find the right solution, but also had no time to check the customers PC thouroughly.... (what version of adobe for instance) Commented Apr 11, 2012 at 9:18
  • By the way: I found one of my customers having the same problem on a windows PC.... Commented Apr 11, 2012 at 9:24

6 Answers 6

5

I had the same problem. But in my case it was Ubuntu 18.04 and java 1.8.0_161-b12 In Windows 10, everything is working fine. But on Ubuntu

Desktop.getDesktop().open(new file) 

the program stopped responding. I decided to wrap the call in the executor:

private ExecutorService executorService; 
   BasicThreadFactory factory = new BasicThreadFactory.Builder()
            .namingPattern("YourPatternIndeficator")
            .build();
    executorService = Executors.newSingleThreadExecutor(factory);
if (Desktop.isDesktopSupported()) {
        File myFile = new File(path);
        executorService.execute(() -> {
            try {
                Desktop.getDesktop().open(myFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

    }
Sign up to request clarification or add additional context in comments.

1 Comment

I am working with JavaFx and Ubuntu 18.04 and this works for me. I did not use the ExecutorService, I just run it in another thread, but the idea works, thank you
1

I was running into what sounds like the same issue on Mint 13. From what I can tell, changes to mime handling for opening directories has broken the java Desktop api. I was able to work around the problem by editing

~/.local/share/applications/defaults.list

and adding this line

x-directory/normal=nautilus.desktop

I'm running Mint 13 Cinnamon with java version "1.7.0_05"

Comments

0

I can't confirm the error. I took your code and constructed a main method around it, and everything works as expected. I don't exactly know where the default applications are set (in my case PCMan was opened instead of usual Nautilus, but it should fulfil its purpose in the end).

Over at java.awt.Desktop.open doesn’t work with PDF files? I have found a link pointing to an issue in Suns (Oracles) bug tracker stating that the method for opening files using AWT isn't reliable even on Windows. Maybe you should think of alternative ways of opening such applications. Furthermore AWT is deprecating soon almost for sure.

If you are utilizing SWT in your application, you could use org.eclipse.swt.program.Program.

4 Comments

Ok, thanks for trying, I normally use nautilus as well, but as a matter of fact, I don't really care what file explorer opens, as long as there opens one..... 1 question to you, because I read a lot before posting this, and I came along a 64 bits Desktop bug.... and I have a 64 bit laptop. Do you use 32 or 64 bit PC?
Thanx for the extra info, I editted my question, opening files isn't a problem, just opening the path on ubuntu. I wasn't aware of the AWT / SWT issue of deprecation... (good to know)
SWT is a modern toolkit used by Eclipse and many others. You can use it without any thoughts of deprecation or whatever. It's just AWT - the underlying technology of Swing - that will deprecate. For more information, read the FAQ entry Is JavaFX replacing Swing as the new client UI library for Java SE?.
Ok, still haven't found the solution, but this gives me a good direction where to search, upvoted already for the information. Thanks untill so far, if this leads me to the solution, I'll let you know.
0

I was running into the same issue and decided to give Java 7 a whirl. I'm running java version "1.7.0_147-icedtea" on Ubuntu 11.10_x64 and am able to open file locations in Nautilus quite happily now.

1 Comment

Thanx for the suggestion, I could give that a try. However... the customer who is having the problem works in windows, and releasing a non-release on a customer's pc.....
0

I have the same issue on my Linux Mint (and not in Windows).

That link helped me : Troubles with java.awt.Desktop browse() method.

This seems to work on my Linux Mint-KDE. I changed the line

Desktop.getDesktop().desktop.open(new File("/home/user/mypath"));// this throws IOException: Failed to show URI (except in Windows)

with

Desktop.getDesktop().desktop.open(new File("///home/user/mypath"));// this launches Dolphin

or with

Desktop.getDesktop().desktop.open(new File(new URI("file:///home/user/mypath").getPath()));// this launches Dolphin

Dolphin was launched with my folder "mypath". But I found no way to open a file like a pdf or txt on my Linux while it works on Windows with the first code.

(Java 1.8.0_25, Netbeans 8.02, Linux Mint 12 KDE)

Comments

0

I have the issue with Kubuntu 18.04 and java 11. It was solved with

sudo apt install libgnome2-0 gvfs

see https://bugs.launchpad.net/ubuntu/+source/openjdk-8/+bug/1574879/comments/5 for details. java.awt.Desktop works with Gnome not with KDE.

Comments

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.