Open a path with Desktop.open() from java on ubuntu (linux)

浪子不回头ぞ 提交于 2021-02-18 05:59:26

问题


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


回答1:


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();
            }
        });

    }



回答2:


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"




回答3:


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:


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.




回答5:


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)




回答6:


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.



来源:https://stackoverflow.com/questions/9692331/open-a-path-with-desktop-open-from-java-on-ubuntu-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!