Java AWT Desktop cannot invoke the system browser

Source: Internet
Author: User

When you build a project with spring boot, you want to automatically talk about first pages after the project starts.

I used the Java.awt.Desktop class.

        if  ( Desktop.isdesktopsupported ())  {             try {                //   Pop-up browser  -  show http interface (HTTPS)                  desktop.getdesktop (). Browse (New uri ("81383628"));             } catch  (exception e)  {                 logger.info (E.getMessage ());             }         } 

The results are normally accessed in the test class and cannot be ejected 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<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 (                                &Nbsp; 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;    }

To troubleshoot the cause, it is found that System.getproperty ("java.awt.headless") in the Getheadlessproperty method returns True when the system parameter is obtained.

Causes the headlessexception exception to be thrown directly. Headless mode is a system configuration in the absence of a display, keyboard, or mouse, which causes the specified window to not pop up.

System.setproperty ("Java.awt.headless", "false");

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


Java AWT Desktop cannot invoke the system browser

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.