Is there a method that will return a user's default browser as a String?
Example of what I am looking for:
System.out.println(getDefaultBrowser()); // prints "Chrome"
Is there a method that will return a user's default browser as a String?
Example of what I am looking for:
System.out.println(getDefaultBrowser()); // prints "Chrome"
You can accomplish this method by using registries[1] and regular expressions to extract the default browser as a string. There isn't a "cleaner" way to do this that I know of.
Now whenever
getDefaultBrowser()
is called, the default browser for Windows should be returned.Tested browsers:
Explanation of the regex (
/(?=[^/]*$)(.+?)[.]
):/(?=[^/]*$)
matches the last occurring/
in the string[.]
matches the.
in the file extension(.+?)
captures the string between those two matched characters.You can see how this is captured by looking at the value of
registry
right before we test it against the regex (I've bolded what is being captured):[1] Windows only. I don't have access to a Mac or Linux computer, but from looking around the Internet, I think
com.apple.LaunchServices.plist
stores the default browser value on a Mac, and on Linux I think you can execute the commandxdg-settings get default-web-browser
to get the default browser. I could be wrong on that though, maybe someone with access to those would be willing to test for me and comment on how to implement them?