Start browser automatically after spring-boot webapp is ready

How to start the browser automatically after starting the spring boot application. Is there any callback method of the listener method to check if webapp has been deployed and is it ready to serve requests so that when the browser loads, the user sees the index page and can start interacting with webapp?

public static void main(String[] args) { SpringApplication.run(Application.class, args); // launch browser on localhost } 
+7
source share
4 answers

You can do this with some Java code. I am not sure if there is anything out of the box when loading spring.

 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(); } } } } 
+4
source

Below code worked for me:

 @EventListener({ApplicationReadyEvent.class}) void applicationReadyEvent() { System.out.println("Application started ... launching browser now"); Browse("www.google.com"); } public static void Browse(String url) { if(Desktop.isDesktopSupported()){ Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(url)); } catch (IOException | URISyntaxException e) { e.printStackTrace(); } }else{ Runtime runtime = Runtime.getRuntime(); try { runtime.exec("rundll32 url.dll,FileProtocolHandler " + url); } catch (IOException e) { e.printStackTrace(); } } } 
+3
source

I recently tried to get this to work on my own, I know it has been a while since this question was asked, but my working (and very simple / simple) solution is shown below. This is the starting point for those who want this to work, refactoring as required in your application!

 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import java.awt.*; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); openHomePage(); } private static void openHomePage() { try { URI homepage = new URI("http://localhost:8080/"); Desktop.getDesktop().browse(homepage); } catch (URISyntaxException | IOException e) { e.printStackTrace(); } } } 
+1
source
 @SpringBootApplication @ComponentScan(basePackageClasses = com.io.controller.HelloController.class) public class HectorApplication { public static void main(String[] args) throws IOException { SpringApplication.run(HectorApplication.class, args); openHomePage(); } private static void openHomePage() throws IOException { Runtime rt = Runtime.getRuntime(); rt.exec("rundll32 url.dll,FileProtocolHandler " + "http://localhost:8080"); } } 
+1
source

Source: https://habr.com/ru/post/979321/


All Articles