Fog Creek Software
Discussion Board




How to launch a browser from Java application?

In applet, you have AppletContext. You can just call showDocument().

Does anybody know if there is a way to do it in applicatioin?

Thanks,

Rover

rover
Monday, April 28, 2003

you can talk to the shell directly as Runtime.getRuntime().exec("shell command launching browser"); but this is a little crude and possible coarse grained, so you may want to include a little VB executable (which would be executed by the exec method) that does it via OLE automation.

Daniel Shchyokin
Monday, April 28, 2003

Also If you can get a hold of MSFTs JVM they may have OLE Automation hooks built into it

Daniel Shchyokin
Monday, April 28, 2003

If you use Microsoft's jvm, you'll be dealing with a 6 year old jvm.

anon
Monday, April 28, 2003

Try http://www.tolstoy.com/samizdat/jconfig.html for a cross-platform method of finding the path of an application for a given file type. You should be able to find the user's preferred browser by looking at the entry for html files.

Scotty
Monday, April 28, 2003

As long as you don't need the newer features your ok, JVMs don't rust anon!

Daniel Shchyokin
Monday, April 28, 2003

For simple browsing you can easily create a browser using the HTMLKit class.

http://java.sun.com/j2se/1.3/docs/api/

http://www.jguru.com/faq/view.jsp?EID=4370

Ged Byrne
Monday, April 28, 2003

Most of the code I've seen to do this comes directly from this JavaWorld article:

http://www.javaworld.com/javaworld/javatips/jw-javatip66.html

This example has some limitations, but works fine for the most common cases.

Rob
Monday, April 28, 2003

Thank you guys! The solution in JavaWorld article seems good enough to me.

However, I just found there is a method javax.jnlp.BasicService.showDocument() in the api document. Anybody know how to use it? I don't mind to deploy my app with WebStart.

rover
Monday, April 28, 2003

Wow. I just did this.  My solution actually runs the default browser on the client for the file type. E.g. pass it a .html or an http://, you get Internet Explorer, pass it a .txt, you get Notepad or whatever.

Disclaimer: for this application, it is guaranteed to be a Windows client.

My solution detects the flavor of Windows and runs command.exe or cmd.exe as appropriate:

String command = commandExe+" /c start \"\" \""+url+"\"";
Process p = rt.exec(command);

where
  commandExe is the proper command statement
  url is the URL passed in, and
  rt is the Runtime as others have described.

I tried using the jnlp services, but since my client app is in Java 1.3.1.04 and Webstart is only standard in Java 1.4+, it didn't seem worth it to attempt to distribute this entire new package for one function. And that package is not really intended for starting web browsers - it's just a nice feature that the package happens to include.

Well, that, and I couldn't get the javax.jnlp.BasicService.showDocument() method to work in my app. :-)  Here's some doc on it, though.

http://www.croftsoft.com/library/tutorials/browser/

HTH.

Lauren B.
Tuesday, April 29, 2003

*  Recent Topics

*  Fog Creek Home