-3

I would like to start the default web browser from Java with defined web page. I tested several example but they are not working. I'm Using JavaFX which may cause this problem. Is there any universal way to start web browser in Java?

Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

3 Answers3

1

Windows-only, but I tried this (with the appropriate try/catch) and it seems to work for me. My default browser is chrome and it just opened chrome and went to that page.

Runtime.getRuntime().exec("start www.google.com")

Are you looking for a cross-platform solution?

MxLDevs
  • 19,048
  • 36
  • 123
  • 194
1

I found a neat way of doing it for cross -platform.Through our java application we can identify the OS and create an executable file - .bat in Windows and .bin in Linux and give it the appropriate executable rights using java.io.File.setExecutable(true,true).

To identify the OS,we can use the following command:-

String operatingSystem = java.lang.System.getProperties().getProperty("os.name");

After creating the file,we add the contents to the file for starting the browser,say start www.google.com for Windows or some other command for Linux or Mac.After we have created the file ,we can call the code:-

Runtime rm = Runtime.getRuntime();

rm.exec("launch_browser.bat");

or

 rm.exec("launch_browser.bin"); 

The launch_browser.bat or launch_browser.bin will call the code to start a http web request and will launch the OS default browser

Kumar Abhinav
  • 6,565
  • 2
  • 24
  • 35
  • 1
    If using a fairly spartan Java implementation consisting of little more than `Runtime.exec`, providing a Windows batch file and Unix/Linux/Mac OS X shell script as this answer recommends is the way to go. Not only is there typically more to successfully launching even the system default web browser than can be specified in a single string passed to `Runtime.exec`, providing a script file wrapper enables local customization of the launch script which may be required in some cases. – Ned Feb 09 '14 at 21:15
0

Look into the BrowserLauncher2 project. It is derived and somewhat updated from the BrowserLauncher class originally written and released by Eric Albert. I used the original BrowserLauncher class successfully in a multi-platform Java application which ran locally with a web browser interface in the early 2000s.

Note that BrowserLauncher2 is licensed under the GNU Lesser General Public License. If that license is unacceptable, look for a copy of the original BrowserLauncher which has a very liberal license:

This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that's more out of curiosity than anything and is not required. This software includes no warranty. The author is not repsonsible for any loss of data or functionality or any adverse or unexpected effects of using this software.

Credits: Steven Spencer, JavaWorld magazine (Java Tip 66) Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk

Projects other than BrowserLauncher2 may have also updated the original BrowserLauncher to account for changes in browser and default system security settings since 2001.

Ned
  • 937
  • 6
  • 10