views:

2444

answers:

4

From a Java program, I need to launch the default browser on a local HTML file, pointed to an anchor inside the file. In Java SE 6, the java.awt.Desktop.browse method will open the file, but will not honor the anchor, so something like the following opens the file at the top, but does not page the browser to the anchor:

Desktop.getDesktop("file:///C:/foo/bar.html#anchor");

Sun says here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477862 that anchors are not supported in the file URI protocol.

Does anyone have a better answer?

I can use Java SE 6. I would be OK with a Windows only solution.

A: 

how about the technique described here?

http://stackoverflow.com/questions/242579/limit-for-url-length-for-rundll32-urldllfileprotocolhandler

cmsherratt
That method loses the anchor. The browser opens at the top of the file.
Jeff C
+1  A: 

For Windows only, you could try

System.exec("cmd.exe start file:///C:/foo/bar.html#anchor")
Software Monkey
A: 

You could try using BrowserLauncher2. It's a small and self-contained cross-platform library to open the default browser. It handles anchors perfectly.

amarillion
A: 

I tried 'BrowserLauncher2'. It doesnt support anchors like all other solutions (rundll, Desktop.getDesktop, ...)

Black Adder