2

I have a program (GUI = JavaFX), which scans my file system for specific XML report files and lists them in a table. These XML reports are rendered in a web browser by XSLT. Now I want to be able to click on such a report in my Java application and have it displayed in the browser. I already wrote the handler and the correct URL is determined. On my Windows system this is

file://localhost/C:/report.xml

The XML is not the problem. If I open it manually in my browser everything works fine. However if I use Google and ask how to open files in a browser it always gives me this:

java.awt.Desktop.getDesktop().
    browse(new java.net.URI("file://localhost/C:/report.xml"));

As this is a good solution for http URLs (web sites), it always opens my XML file in my default text editor (e.g. Notepad++). So the browse method of the Desktop doesn't really force the browse, but merely falls back to a default open operation.

So the question is: How can I force Java to open the XML in the browser similar to the Windows function "Open with >"?

Here an sscce (which should try to access the file in a browser, even though it doesn't exist):

public class XMLOpener {
  public static void main(String[] args) 
  {
    String fileURL = "file://localhost/C:/report.xml";
    try {
      java.awt.Desktop.getDesktop().browse(new java.net.URI(fileURL));
    } catch (Exception e) {}
  }
}
Peter Ilfrich
  • 3,727
  • 3
  • 31
  • 35
  • For better help sooner, please include an [sscce](http://www.sscce.org). – user1329572 Aug 22 '12 at 12:40
  • Thanks for the sscce. What does [`Desktop.isDesktopSupported()`](http://docs.oracle.com/javase/7/docs/api/java/awt/Desktop.html#isDesktopSupported%28%29) return? – user1329572 Aug 22 '12 at 12:50
  • Desktop.isDesktopSupported() returns true. If I try java.awt.Desktop.getDesktop().browse(new java.net.URI("http://www.google.com")); it opens the browser as expected. – Peter Ilfrich Aug 22 '12 at 12:52
  • Thanks, Peter. I'm assuming you've read the API, but if you haven't: "**If the default browser is not able to handle the specified URI, the application registered for handling URIs of the specified type is invoked**". – user1329572 Aug 22 '12 at 12:54
  • Yes, I did. But apparently my browser does support XML (as every browser does - even IE! ;-)), since I can open the URL manually. It's just not the default application for XML. – Peter Ilfrich Aug 22 '12 at 12:56
  • Peter, [this](http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/desktop_api/) may be of some help. – user1329572 Aug 22 '12 at 12:59
  • Thanks. This demo unfortunately has the same effect as my code. Seems like my only option is to launch an available browser manually and pass the URI as parameter :( – Peter Ilfrich Aug 22 '12 at 13:04
  • That's too bad :(. Gives you an opportunity to write your own utility method though ;]! – user1329572 Aug 22 '12 at 13:06

2 Answers2

1

If the browser can't handle the URI, browse launches "the application registered for handling URIs of the specified type is invoked".

Check your favourite browser exists (Chrome, Fx, O, IE, etc) or get it by some other method, and then execute a custom command. If you know the OS you're running on (windows) then you only need to consider the exec line for that.

Community
  • 1
  • 1
Paul S.
  • 64,864
  • 9
  • 122
  • 138
1

The JavaFX replacement for the awt Desktop.browse method would be HostServices.showDocument. You could try that, but it will likely have the same the same effect as Desktop.browse.

Another alternative is to load the XML and perform the XSL transform in Java, then display the resulting document in a JavaFX WebView using webview.getEngine.loadContent(contentString, contentType) or just display the resultant document in a Label or custom JavaFX control. Note that, as of JavaFX 2.2, the JavaFX WebView does not yet have a viewer for pretty printing an xml content type, so to get pretty printed xml in the webview you may need to parse and format the xml as an html document using javascript/css/html, similar to the method demonstrated in this post for displaying formatted java source in a WebView.

For me, although it's more development work, this alternate approach of handling the display with JavaFX is nice because the display of the resultant document can be encapsulated and controlled in the JavaFX application itself and you don't have a reliance on whatever browser and configuration may or may not be installed in the host environment.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • HostServices.showDocument has the same effect. As for displaying the content inside the application: for the function in question the implementation effort is too high. But thanks for the advice. – Peter Ilfrich Aug 24 '12 at 10:30