在 ubuntu (linux) 上从 java 打开 Desktop.open() 路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9692331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:14:41  来源:igfitidea点击:

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

javaubuntupathdesktop

提问by michel.iamit

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

从我用 java 编写的应用程序中,我想使用操作系统文件资源管理器打开一个文件夹。

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

我使用 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.

这在 Windows 上运行良好,但在 ubuntu 11.10 (linux) 上不起作用。使用 Desktop.open 打开文件在 ubuntu 和 windows 上都有效。

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

使用两者之间的一个步骤: File fPath=new File(fPath) 并使用 fPath.exists() 和 fPath.isDirectory() 对其进行测试,两者都为真。

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

使用 Desktop.open(new File(path)) 给了我这个例外:

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

我还不能在苹果电脑上测试这个,但我希望 Desktop.open(new File(path)) 是系统独立的.....

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)

一些额外信息:操作系统:Linux(3.0.0-16-generic - amd64)

Java: 1.6.0_30-b12

Java:1.6.0_30-b12

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

Java主页:/opt/java/64/jre1.6.0_30

回答by f4lco

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

我无法确认错误。我接受了你的代码并围绕它构建了一个主要方法,一切都按预期工作。我不完全知道默认应用程序的设置位置(在我的情况下,PCMan 被打开而不是通常的 Nautilus,但它最终应该实现其目的)。

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

java.awt.Desktop.open 不能处理 PDF 文件?我发现一个链接指向Suns (Oracles) 错误跟踪器中的一个问题,指出使用 AWT 打开文件的方法即使在 Windows 上也不可靠。也许您应该考虑打开此类应用程序的其他方法。此外,几乎可以肯定,AWT 很快就会弃用。

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

如果您在应用程序中使用 SWT,则可以使用org.eclipse.swt.program.Program.

回答by James

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.

我遇到了同样的问题,并决定试一试 Java 7。我在 Ubuntu 11.10_x64 上运行 Java 版本“1.7.0_147-icedtea”,现在可以很高兴地在 Nautilus 中打开文件位置。

回答by esigler

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

我在 Mint 13 上遇到了听起来同样的问题。据我所知,打开目录时对 mime 处理的更改破坏了 java Desktop api。我能够通过编辑解决这个问题

~/.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"

我正在使用 Java 版本“1.7.0_05”运行 Mint 13 Cinnamon

回答by fanatek

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

我的 Linux Mint(而不是 Windows)有同样的问题。

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

该链接对我帮助: java.awt.Desktop browse() 方法有问题

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

这似乎适用于我的 Linux Mint-KDE。我换了线

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.

Dolphin 是用我的文件夹“mypath”启动的。但是我发现无法在我的 Linux 上打开像 pdf 或 txt 这样的文件,而它在 Windows 上使用第一个代码工作。

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

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

回答by Roman Kuznetsov

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

我有同样的问题。但就我而言,它是 Ubuntu 18.04 和 java 1.8.0_161-b12 在 Windows 10 中,一切正常。但是在 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();
            }
        });

    }

回答by modulo

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

我有 Kubuntu 18.04 和 java 11 的问题。它已解决

sudo apt install libgnome2-0 gvfs

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

有关详细信息,请参阅https://bugs.launchpad.net/ubuntu/+source/openjdk-8/+bug/1574879/comments/5。java.awt.Desktop 适用于 Gnome 而不是 KDE。