Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java awt Desktop cannot call the system browser

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

When building a project with Spring boot, I hope to automatically talk about the home page after the project is started.

I used the java.awt.Desktop class.

If (Desktop.isDesktopSupported ()) {try {/ / pop-up browser-displays the HTTP interface (https) Desktop.getDesktop () .browse (new URI ("https://blog.csdn.net/weixin_42156742/article/details/81383628"));} catch (Exception e) {LOGGER.info (e.getMessage () }}

As a result, it can be accessed normally in the test class, but the web page cannot be popped up after starting the project.

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) ()-> {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 () .equals ()) } else {headless = Boolean.valueOf (nm);} return null;});} return headless;}

Dig down and find that true is returned when the system parameter is obtained at System.getProperty ("java.awt.headless") in the getHeadlessProperty method.

Causes a HeadlessException exception to be thrown directly. Headless mode is the system configuration when the display, keyboard, or mouse is missing, which is because the parameters here prevent the specified window from popping up.

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

So you need to set the parameter to false in advance.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report