Launch browser automatically after spring-boot webapp is ready

后端 未结 6 885
失恋的感觉
失恋的感觉 2020-12-15 09:43

How do I launch a browser automatically after starting the spring boot application.Is there any listener method callback to check if the webapp has been deployed and is read

6条回答
  •  独厮守ぢ
    2020-12-15 10:31

    You could do it by some java code. I am not sure if spring boot has something out of the box.

    import java.awt.Desktop;
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class Browser {
        public static void main(String[] args) {
            String url = "http://www.google.com";
    
            if(Desktop.isDesktopSupported()){
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.browse(new URI(url));
                } catch (IOException | URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }else{
                Runtime runtime = Runtime.getRuntime();
                try {
                    runtime.exec("xdg-open " + url);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    

提交回复
热议问题