1

I am launching a URL using the following code in Java:

Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler " + myURL)

Here myURL is "file:///C:/Desktop/test.html#bottom"

When I am passing the URL like this, it's only executing "file:///C:/Desktop/test.html"

It's not including #bottom.

How do I correctly launch this URL in Java?

glenneroo
  • 1,908
  • 5
  • 30
  • 49
shree
  • 2,745
  • 7
  • 28
  • 35

2 Answers2

3

Another way to do it (from How to launch a file protocol URL with an anchor from Java? ):

Runtime.getRuntime().exec("cmd.exe start file:///C:/foo/bar.html#anchor")

Update:

Yet another link with this issue (using url.dll) shows the author reverting to trying an external program, in this case Quero: Runtime.getRuntime().exec("cmd.exe qlaunch.exe open=\"file:///c:/temp/test.html?p=1\"");

From what I can tell, this is a bug in Windows and/or Internet Explorer in particular. Firefox has other issues with the file:// protocol that you have to manually disable.


Update 2:

This is definitely either expected behavior or a bug in Windows itself. Using Desktop.browse (which sends the URI clean to ShellExecute) or cmd.exe start or Runtime.getRuntime().exec("rundll32 url.dll " + uri) or even just opening a command prompt and typing the file:// URI itself just ends up doing this (which I found via Process Monitor):

Process Monitor screenshot

Order of operations shown:

  1. (1st box) Thread Create = when I hit enter in the cmd window above
  2. Look for the file by traversing the directory tree (e.g. go into c:, go into temp)
  3. (2nd box) When that file is found, STOP looking (which means Windows parsed off the # and ? characters)
  4. (3rd box) DONE looking (CloseFile on parent folder, followed by no more disk I/O)
  5. (4th box) Now that the file is found, look to see what .htm files are opened with (e.g. your browser)
  6. Oddly enough, later on it finds Chrome (default browser) but runs IE (?!) and then runs Chrome - this might be a big part of the problem.

Note that calling start iexplore file:///c:/temp/test.html#mark does work because it's just passing the parameter without trying to do anything special.

Note that steps 2 and 3 violate Microsoft's own advice from 2006 (see here) but from what I've seen this behavior was introduced at the time that IE7 was released. My guess is that this fixed some bug or security flaw in Windows at the cost of being able to do this at all.

With some more research, it appears that file: URIs in Windows have had a very sordid past

Oddly enough, a Microsoft article from 2010 shows file://host/path/file.txt?Query#fragment as a valid URI in .Net...

I think you might be out of luck until someone finds a magical Registry setting - or you can manually call a browser (e.g. Runtime.getRuntime().exec(chromepath + "chrome.exe file:///c:/temp/test.html#jump") or Runtime.getRuntime().exec(iepath + "iexplore.exe file:///c:/temp/test.html#jump"))

Or, for fun: Runtime.getRuntime().exec("cmd.exe start iexplore " + myURL)

Community
  • 1
  • 1
Adam Rofer
  • 6,121
  • 1
  • 14
  • 11
  • I tried..Its not working.System.exec is not available.And I tried with %23 also.but its not working. – shree Feb 08 '12 at 03:56
  • Also in the linked item is a suggestion to surround the URL in quotes: `Runtime.getRuntime().exec("rundll32 url.dll , FileProtocolHandler \"" +myURL + "\"")` – Adam Rofer Feb 08 '12 at 04:06
  • Comment for the other answer below: Looks like URL parameters using `?` on Windows 7 can't be passed to `Desktop.browse()`: http://stackoverflow.com/questions/5407189/how-to-pass-url-parameters-from-java-to-local-html-file-in-windows-7 – Adam Rofer Feb 08 '12 at 04:42
  • But am not using windows 7...I am working on windows XP only,,, – shree Feb 08 '12 at 04:51
  • There was a bug in 2010 similar to this in .NET: http://stackoverflow.com/questions/8757585/why-doesnt-system-uri-recognize-query-parameter-for-local-file-path - the cryptic "Unfortunately, we cannot fix this bug due to app-compat issues" leads me to think this is a Windows issue, even in XP... – Adam Rofer Feb 08 '12 at 04:57
  • @AdamRofer (scratches head) I thought the problem was anchors (`#`) rather than query parameters (`?`). – Andrew Thompson Feb 08 '12 at 05:44
  • It is (the term is fragment for anchor tag URL components) but I think this problem occurs at the same time for both in all instances - in some implementations of the `file://` protocol I've seen, they simply look up the file and toss the parameters (against the URI spec). Here's a KDE.org argument on how to approach this: http://lists.freedesktop.org/archives/xdg/2008-March/009369.html – Adam Rofer Feb 08 '12 at 05:59
0

Use Desktop.browse(URI) (which has a chance of working on *nix and OS X as well as Windows).


Update:

OK - scratch that. The net based URIs will scroll to the anchor, disk based URIs will not. Raised a bug report with Oracle (ID: 7143677 - Desktop.browse() - Anchors are ignored for local URIs).

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    Looks like they refused to fix it with bug ([6477862](http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6477862) - _Local files with anchors do not work with Desktop.browse()_) – Adam Rofer Feb 08 '12 at 19:09