Can Java's Desktop library launch a URL in a new Browser Tab or Window?

后端 未结 5 1144
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 14:49

I have a static method used to launch a browser with a given URL. When the browser is already open, this takes over the active browser window.

This is a problem if t

5条回答
  •  [愿得一人]
    2020-12-06 15:35

    Under Windows, there is a way to find what the default browser is. You will have to use the commands

    REG QUERY HKCR\.html /ve
    

    which would return

    ! REG.EXE VERSION 3.0
    
    HKEY_CLASSES_ROOT\.html
           REG_SZ  FirefoxHTML
    

    Then you would query FirefoxHTML (or whatever value was returned) using the same command, and append the following to the key

    REG QUERY HKCR\FirefoxHTML\shell\open\command /ve
    

    and this would return

    ! REG.EXE VERSION 3.0
    
    HKEY_CLASSES_ROOT\FirefoxHTML\shell\open\command
           REG_SZ  "C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"
    

    From here, you can parse the returned lines to grab the location of the browser executable.

    You can do all this using the Java Runtime class:

    Runtime.getRuntime.exec(cmdString);
    

    This requires a lot of customized coding, but you could essentially create your own API to access the default browser under Windows.

    Here is an example of accessing the Windows registry in Java.

    You could also search Google for more information on finding default browser in the registry.

    As for other platforms (Mac, Linux, etc), I am sure there is a specific way to get the path to the default browser.

    Hope this helps!

提交回复
热议问题