views:

49

answers:

2

I am using

java.awt.Desktop.getDesktop().browse(uri);

to show my users a generated html file. All is fine if the browser (firefox 3.5.7; linux) is started before I make the call. But if the browser start is triggered from the getDesktop().browse call then the java application will not exit until the browser closes.

How can I avoid this behaviour? Is this known under windows/macOsx too?

(If I then force the exit of the java application the browser will close too and sometime even crash!?)

+2  A: 

The reason the browser exists is because the browser is launched as a dependent process...so when the first process shuts down, all its dependent processes are shut down with it. But in the case when you start the browser first, it already has a different process ID that isn't affected by your application. I think the only way to avoid this behavior is to use a different technique (perhaps Runtime.exec()?) to launch the browser in a way that registers it as a non-dependent process.

Jessica
Yes, thanks! that did the trick. I am usining now the code here:http://www.centerkey.com/java/browser/ (of course without the fallback version to jse6 or I will fallback to jse6 if the other calls do not work :-))
Karussell
A: 

The project Browser Launcher (http://browserlaunch2.sourceforge.net/) can be the solution for your problem. You can launch a browser from your code like this:

String url = "http://....";
BrowserLauncher() launcher = new edu.stanford.ejalbert.BrowserLauncher();
launcher.openURLinBrowser(url);
cuh