Java 当前平台不支持桌面 API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18004150/
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-11 21:42:39  来源:igfitidea点击:

Desktop API is not supported on the current platform

javaapifiledesktop-application

提问by user2520969

I have encountered this error:

我遇到了这个错误:

java.lang.UnsupportedOperationException: Desktop API is not supported on the current platform

java.lang.UnsupportedOperationException: 当前平台不支持桌面 API

I would open a file from my java application. I use this method:

我会从我的 java 应用程序打开一个文件。我用这个方法:

Desktop.getDesktop().open(new File(report.html"));

How can i solve this problem?

我怎么解决这个问题?

采纳答案by MightyPork

Basically, the problem is that Java Desktop integration doesn't work well on Linux.

基本上,问题在于 Java 桌面集成在 Linux 上不能很好地工作。

It was designed to work good with Windows; something works on other systems, but nobody really cared to add proper support for those. Even if you install the required 'gnome libraries', the results will be poor.

它旨在与 Windows 配合使用;有些东西适用于其他系统,但没有人真正关心为这些添加适当的支持。即使您安装了所需的“gnome 库”,结果也会很差。

I've faced the very same problem a while ago, and came up with the class below.

不久前我遇到了同样的问题,并想出了下面的课程。

The goal is achieved by using system-specific commands:

该目标是通过使用特定系统的命令来实现的:

KDE:     kde-open
GNOME:   gnome-open
Any X-server system: xdg-open
MAC:     open
Windows: explorer

If none of those works, it tries the implementation provided by Java Desktop.
Because this one usually fails, it's tried as the last resort.

如果这些都不起作用,它会尝试 Java Desktop 提供的实现。
因为这个通常会失败,所以它被作为最后的手段尝试。



DesktopApi class

DesktopApi 类

This class provides static methods open, browseand edit.
It is tested to work on Linux (Kde and Gnome), Windows and Mac.

这个类提供静态方法openbrowseedit
它经过测试可在 Linux(Kde 和 Gnome)、Windows 和 Mac 上运行。

If you use it, please give me credit.

如果你使用它,请给我信用。

package net.mightypork.rpack.utils;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;


public class DesktopApi {

    public static boolean browse(URI uri) {

        if (openSystemSpecific(uri.toString())) return true;

        if (browseDESKTOP(uri)) return true;

        return false;
    }


    public static boolean open(File file) {

        if (openSystemSpecific(file.getPath())) return true;

        if (openDESKTOP(file)) return true;

        return false;
    }


    public static boolean edit(File file) {

        // you can try something like
        // runCommand("gimp", "%s", file.getPath())
        // based on user preferences.

        if (openSystemSpecific(file.getPath())) return true;

        if (editDESKTOP(file)) return true;

        return false;
    }


    private static boolean openSystemSpecific(String what) {

        EnumOS os = getOs();

        if (os.isLinux()) {
            if (runCommand("kde-open", "%s", what)) return true;
            if (runCommand("gnome-open", "%s", what)) return true;
            if (runCommand("xdg-open", "%s", what)) return true;
        }

        if (os.isMac()) {
            if (runCommand("open", "%s", what)) return true;
        }

        if (os.isWindows()) {
            if (runCommand("explorer", "%s", what)) return true;
        }

        return false;
    }


    private static boolean browseDESKTOP(URI uri) {

        logOut("Trying to use Desktop.getDesktop().browse() with " + uri.toString());
        try {
            if (!Desktop.isDesktopSupported()) {
                logErr("Platform is not supported.");
                return false;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
                logErr("BROWSE is not supported.");
                return false;
            }

            Desktop.getDesktop().browse(uri);

            return true;
        } catch (Throwable t) {
            logErr("Error using desktop browse.", t);
            return false;
        }
    }


    private static boolean openDESKTOP(File file) {

        logOut("Trying to use Desktop.getDesktop().open() with " + file.toString());
        try {
            if (!Desktop.isDesktopSupported()) {
                logErr("Platform is not supported.");
                return false;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
                logErr("OPEN is not supported.");
                return false;
            }

            Desktop.getDesktop().open(file);

            return true;
        } catch (Throwable t) {
            logErr("Error using desktop open.", t);
            return false;
        }
    }


    private static boolean editDESKTOP(File file) {

        logOut("Trying to use Desktop.getDesktop().edit() with " + file);
        try {
            if (!Desktop.isDesktopSupported()) {
                logErr("Platform is not supported.");
                return false;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.EDIT)) {
                logErr("EDIT is not supported.");
                return false;
            }

            Desktop.getDesktop().edit(file);

            return true;
        } catch (Throwable t) {
            logErr("Error using desktop edit.", t);
            return false;
        }
    }


    private static boolean runCommand(String command, String args, String file) {

        logOut("Trying to exec:\n   cmd = " + command + "\n   args = " + args + "\n   %s = " + file);

        String[] parts = prepareCommand(command, args, file);

        try {
            Process p = Runtime.getRuntime().exec(parts);
            if (p == null) return false;

            try {
                int retval = p.exitValue();
                if (retval == 0) {
                    logErr("Process ended immediately.");
                    return false;
                } else {
                    logErr("Process crashed.");
                    return false;
                }
            } catch (IllegalThreadStateException itse) {
                logErr("Process is running.");
                return true;
            }
        } catch (IOException e) {
            logErr("Error running command.", e);
            return false;
        }
    }


    private static String[] prepareCommand(String command, String args, String file) {

        List<String> parts = new ArrayList<String>();
        parts.add(command);

        if (args != null) {
            for (String s : args.split(" ")) {
                s = String.format(s, file); // put in the filename thing

                parts.add(s.trim());
            }
        }

        return parts.toArray(new String[parts.size()]);
    }

    private static void logErr(String msg, Throwable t) {
        System.err.println(msg);
        t.printStackTrace();
    }

    private static void logErr(String msg) {
        System.err.println(msg);
    }

    private static void logOut(String msg) {
        System.out.println(msg);
    }

    public static enum EnumOS {
        linux, macos, solaris, unknown, windows;

        public boolean isLinux() {

            return this == linux || this == solaris;
        }


        public boolean isMac() {

            return this == macos;
        }


        public boolean isWindows() {

            return this == windows;
        }
    }


    public static EnumOS getOs() {

        String s = System.getProperty("os.name").toLowerCase();

        if (s.contains("win")) {
            return EnumOS.windows;
        }

        if (s.contains("mac")) {
            return EnumOS.macos;
        }

        if (s.contains("solaris")) {
            return EnumOS.solaris;
        }

        if (s.contains("sunos")) {
            return EnumOS.solaris;
        }

        if (s.contains("linux")) {
            return EnumOS.linux;
        }

        if (s.contains("unix")) {
            return EnumOS.linux;
        } else {
            return EnumOS.unknown;
        }
    }
}

回答by Modus Tollens

The Desktop class is not supported on all systems.

并非所有系统都支持 Desktop 类。

From the Java Swing tutorial How to Integrate with the Desktop Class:

从 Java Swing 教程如何与桌面类集成

Use the isDesktopSupported() method to determine whether the Desktop API is available. On the Solaris Operating System and the Linux platform, this API is dependent on Gnome libraries. If those libraries are unavailable, this method will return false. After determining that the Desktop API is supported, that is, the isDesktopSupported() returns true, the application can retrieve a Desktop instance using the static method getDesktop().

使用 isDesktopSupported() 方法确定桌面 API 是否可用。在 Solaris 操作系统和 Linux 平台上,此 API 依赖于 Gnome 库。如果这些库不可用,则此方法将返回 false。在确定支持 Desktop API 后,即 isDesktopSupported() 返回 true,应用程序可以使用静态方法 getDesktop() 检索 Desktop 实例。

In any case, it would be best to provide an alternative way to open a file if there is no support for Desktop.

在任何情况下,如果不支持桌面,最好提供另一种打开文件的方法。

回答by Mark McDonald

Support varies between implementations on the various JDKs. I encountered the "UnsupportedOperationException" using OpenJDK 1.7.0. Switching to the Oracle JDK 1.7 worked.

各种 JDK 上的实现之间的支持各不相同。我在使用 OpenJDK 1.7.0 时遇到了“UnsupportedOperationException”。切换到 Oracle JDK 1.7 有效。

Where practical, you may be able to switch JDKs or suggest that your users switch JDKs to enable a certain feature.

在可行的情况下,您可以切换 JDK 或建议您的用户切换 JDK 以启用特定功能。

回答by John

I am using Ubuntu 12.04 LTS 64-bit with Oracle jdk1.6.0_45 and was having the same problem. I'm running gnome-classic as the desktop instead of Unity. This is what worked for me:

我在 Oracle jdk1.6.0_45 上使用 Ubuntu 12.04 LTS 64 位并且遇到了同样的问题。我运行 gnome-classic 作为桌面而不是 Unity。这对我有用:

sudo apt-get install libgnome2-0

After installing this package I restarted my Java Swing app and Desktop.getDesktop().open(new File("myfile"));worked just fine.

安装这个包后,我重新启动了我的 Java Swing 应用程序并且Desktop.getDesktop().open(new File("myfile"));工作得很好。