Kill opened IE, when killing Swing Java App
I am creating menu items which fire off an IE window to a URL. However, when app is closed the Java process that runs the app blocks waiting for the executed browser to terminate before its shuts itself down. This has potential repercussions as the Java VM hangs around in the mean time, holding resources. Can anyone point me in the direction of how to kill the opened IE window when I kill the java app? I would greatly appreciate any help. :-)
This is the code in question:
userManualMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
String url = System.getProperty("EASEUserManualURL");
Runtime r= Runtime.getRuntime();
Process p= r.exec("iexplore " + url);
}
catch(Exception exception)
{
Logger.getLogger().logError("Error opening Intenet Explorer to show EASE user manual.\nCause: " + exception.getMessage());
}
}
});
When the app is killed I am assuming you have some listener triggered (Window Closed or a ActionPerformed listener triggered somewhere, have that listener call destroy on your IE processes below.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Process.html#destroy()
That will kill your launched IE processes. You will have to keep references to your launched IE subprocesses around somewhere as well so you can reference and kill them later when your user wants to close the app.
Hope that helps
Now I actually read the source code that makes sense ;o)
I think I might still find it irritating as a user, though - particularly if I had navigated to some external page. Actually, I'd find the whole thing irritating since I use Mozilla and don't want some dopey app forcing me to use IE :o)
I agree that FF would be a much better idea (i use it myself) but when working for a large corporate company who only use IE (some of their web apps don't work in anything else) and the fact that my users are IE aware this the reason behind IE.
The fact that the VM runs in the background when the java app is closed means that I either have to kill IE or stop the VM from running.
As I am new to Java I'm stuck! lol
> Wouldn't it solve the problem if you made the exec() call on a daemon
> thread? I wouldn't advise making exec() calls on the EDT in any case.
1 - You can't call exec() on a Daemon thread. You can call Runtime.exec() inside of a spawned thread that has been marked as a Daemon. This would be preferably rather than launching off of the EventDispatcherThread as itchystratchy stated because....
http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
> I'm sure there's a way you can just launch the user's default browser isn't there?
2. - I use http://browserlaunch2.sourceforge.net/in my current app, it launches whatever application you have associated for that file type to view that file. Also supposedly cross platform. You will not get Process reference to the launched app though. I've tested successfully on Win 2000 and XP up to now but it will allow you to close your Java application and leave any launched browsers open if that is what you are looking for or as you asked for earlier keep a collection of launched Process references and destory them when your user is closing your app.