Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ubuntu(linux)上使用java中的Desktop.Open()打开路径_Java_Ubuntu_Path_Desktop - Fatal编程技术网

在ubuntu(linux)上使用java中的Desktop.Open()打开路径

在ubuntu(linux)上使用java中的Desktop.Open()打开路径,java,ubuntu,path,desktop,Java,Ubuntu,Path,Desktop,从java编写的应用程序中,我想使用操作系统文件资源管理器打开一个文件夹 我使用Desktop.open(新文件(路径)) 这在windows上运行良好,但在Ubuntu11.10(linux)上不起作用。 在ubuntu和windows上,使用Desktop.open打开文件都可以 使用介于以下两者之间的步骤: 文件fPath=新文件(fPath) 使用fPath.exists()和fPath.isDirectory()对其进行测试,结果都为true 使用Desktop.open(新文件(路径

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

我使用Desktop.open(新文件(路径))

这在windows上运行良好,但在Ubuntu11.10(linux)上不起作用。 在ubuntu和windows上,使用Desktop.open打开文件都可以

使用介于以下两者之间的步骤: 文件fPath=新文件(fPath) 使用fPath.exists()和fPath.isDirectory()对其进行测试,结果都为true

使用Desktop.open(新文件(路径))会出现以下异常:

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)
我还不能在苹果电脑上测试,但我希望Desktop.open(新文件(路径))是独立于系统的

顺便说一下,完整的代码:

    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;
    }
一些额外信息: 操作系统:Linux(3.0.0-16-generic-amd64)

Java:1.6.0_30-b12


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

我无法确认错误。我接受了您的代码并围绕它构建了一个主方法,一切都按预期进行。我不知道默认应用程序设置在哪里(在我的例子中,PCMan是打开的,而不是通常的Nautilus,但它最终应该实现它的目的)

在上面,我发现了一个链接,指出使用AWT打开文件的方法即使在Windows上也不可靠。也许你应该考虑其他打开此类应用程序的方法。此外,几乎可以肯定的是,AWT很快就会遭到抨击


如果您在应用程序中使用SWT,您可以使用。

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

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

~/.local/share/applications/defaults.list
加上这一行

x-directory/normal=nautilus.desktop

我用java版本“1.7.0_05”运行Mint 13肉桂我的Linux Mint(而不是Windows)也有同样的问题

这个链接帮助我:

这似乎对我的LinuxMintKDE有效。 我换了线路

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

或与

Desktop.getDesktop().desktop.open(new File(new URI("file:///home/user/mypath").getPath()));// this launches Dolphin
Dolphin是用我的文件夹“mypath”启动的。但我发现在我的Linux上无法打开pdf或txt这样的文件,因为它在Windows上使用第一个代码


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

我也遇到了同样的问题。但就我而言,它是Ubuntu 18.04和java 1.8.0_161-b12 在Windows10中,一切正常。但是在Ubuntu上

Desktop.getDesktop().open(new file) 
程序停止响应。 我决定在遗嘱执行人处结束通话:

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

    }

我对Kubuntu 18.04和java 11有问题。这件事用一个简单的方法解决了

sudo apt install libgnome2-0 gvfs

有关详细信息,请参阅。java.awt.Desktop与Gnome一起工作,而不是与KDE一起工作。

顺便说一句,最终代码可以更加紧凑,我使用了更多的行来构建一些额外的检查来调试这个。。。。getDesktop().open(新文件(路径));应该在一行中完成这个技巧。。。试着抓住它,不会受伤的。还有Desktop.isDesktopSupported()catch。仍然没有找到正确的解决方案,但也没有时间仔细检查客户的电脑。。。。(比如adobe的哪个版本)顺便说一句:我发现我的一位客户在windows PC上也有同样的问题……好的,谢谢你的尝试,我通常也使用nautilus,但事实上,我并不在乎文件浏览器打开了什么,只要有一个打开的。。。。。我想问你一个问题,因为我在发布这篇文章之前读了很多书,我发现了一个64位的桌面bug。。。。我有一台64位的笔记本电脑。你使用32位还是64位的电脑?Thanx关于额外的信息,我编辑了我的问题,打开文件不是问题,只是打开ubuntu上的路径。我不知道AWT/SWT的弃用问题。。。(很高兴知道)SWT是Eclipse和其他许多人使用的现代工具包。你可以使用它,而不需要任何反对或其他想法。这只是AWT——Swing的底层技术——将遭到抨击。有关更多信息,请阅读FAQ条目。好的,仍然没有找到解决方案,但这为我提供了一个很好的搜索方向,该信息已被提升。到目前为止,谢谢你,如果这让我找到了解决方案,我会让你知道的。谢谢你的建议,我可以试试。然而。。。出现问题的客户在windows中工作,并在客户的pc上发布非版本。。。。。
sudo apt install libgnome2-0 gvfs