Java awt Desktop cannot call the system browser
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.