Java awt Desktop 无法调用系统浏览器

在Spring Boot项目中使用Java.awt.Desktop尝试自动打开网页时,遇到因System.getProperty("java.awt.headless")返回true导致的HeadlessException异常。问题根源在于系统配置为Headless模式,缺少显示器支持。解决方案是提前将该参数设置为false。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Desktop调用系统浏览器

用Spring boot搭建项目时,希望在项目启动完后能自动谈出首页。

就用了java.awt.Desktop类


        if (Desktop.isDesktopSupported()) {
            try {
                // 弹出浏览器 - 显示HTTP接口(https)
                Desktop.getDesktop().browse(new URI("https://blog.youkuaiyun.com/weixin_42156742/article/details/81383628"));
            } catch (Exception e) {
                LOGGER.info(e.getMessage());
            }
        }

结果在测试类里可以正常访问,在启动项目后却无法弹出网页。

public static synchronized Desktop getDesktop(){
        if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
        if (!Desktop.isDesktopSupported()) {
            throw new UnsupportedOperationException("Desktop API is not " +
                                                    "supported on the current platform");
        }

        sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
        Desktop desktop = (Desktop)context.get(Desktop.class);

        if (desktop == null) {
            desktop = new Desktop();
            context.put(Desktop.class, desktop);
        }

        return desktop;
    }
private static boolean getHeadlessProperty() {
        if (headless == null) {
            AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
                String nm = System.getProperty("java.awt.headless");

                if (nm == null) {
                    /* No need to ask for DISPLAY when run in a browser */
                    if (System.getProperty("javaplugin.version") != null) {
                        headless = defaultHeadless = Boolean.FALSE;
                    } else {
                        String osName = System.getProperty("os.name");
                        if (osName.contains("OS X") && "sun.awt.HToolkit".equals(
                                System.getProperty("awt.toolkit")))
                        {
                            headless = defaultHeadless = Boolean.TRUE;
                        } else {
                            final String display = System.getenv("DISPLAY");
                            headless = defaultHeadless =
                                ("Linux".equals(osName) ||
                                 "SunOS".equals(osName) ||
                                 "FreeBSD".equals(osName) ||
                                 "NetBSD".equals(osName) ||
                                 "OpenBSD".equals(osName) ||
                                 "AIX".equals(osName)) &&
                                 (display == null || display.trim().isEmpty());
                        }
                    }
                } else {
                    headless = Boolean.valueOf(nm);
                }
                return null;
            });
        }
        return headless;
    }

往下排查原因,发现 getHeadlessProperty 方法中 System.getProperty("java.awt.headless") 处获取系统参数时返回了true。

导致直接抛出了HeadlessException异常。Headless模式是在缺少显示屏、键盘或者鼠标时的系统配置,这是此处的参数导致了无法弹出指定窗口。

System.setProperty("java.awt.headless", "false");

所以需要提前设置参数为false。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值