Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which way is better to open browser in eclipse?

Tags:

java

eclipse

swt

I have found 2 ways to open a url in an external browser in an Eclipse application.

Any clues on which way is better? (portability, reliability, ...)

1- simple straight forward use System default:

org.eclipse.swt.program.Program.launch(url)

2- Use browser support to open external browser:

PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser().openURL(url)

The code for (1) looks simple but there must be a reason why eclipse provide the BrowserSupport :) Can someone please educate me.

like image 686
mhussein Avatar asked Nov 03 '22 01:11

mhussein


1 Answers

There is no 'better' in this case, if you're just firing off URLs, you can use either mechanism to the same effect.

Firstly, the simple operation will only ever open the system defined external browser, and that's it; it get's handed off and you never get to interact with it at all.

The getBrowserSupport().getExternalBrowser() will allow you to interact with the browser - e.g. the close() call might actually close the browser.

The more likely reason you'll be interacting with getBrowserSupport() is to work with the (potentially supplied) internal browser.

For AWT/Swing apps there's also:

java.awt.Desktop.getDesktop().browse(new URI(url));

which would accomplish the same thing as the Program.launch() call with the url.

The other thing you can do with Program.launch() is to open documents, folders, etc.

like image 64
Petesh Avatar answered Nov 10 '22 19:11

Petesh