Associate file type with Java Swing application

file, file-association, file-extension, java, swing

Solution

Deploy the app. with Java Web Start:

JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update ..

See the file services demo. for an example of a JNLP that declares an interest in the `.zzz` file type.

This is how the association is declared in the JNLP:

<association
  extensions="zzz"
  mime-type="text/sleepytime" />

JWS is supplied by Oracle and works on Windows, *nix & OS X.

..but I would prefer not to use the Java Web Start. Because I would like to have a native installer that just installs the program, without opening a web page or anything. If that's not how (web start) works, then I'm happy to use it.

JWS works over the web or a network. But if you can invoke the opening of an URL (to the JNLP) from the desktop, there does not need to be any browser opened.

Would I do that using `openConnection()` on a URL object?

I would use the `Desktop` or `BasicService` API.

Desktop.getDesktop().browse(uri);

boolean result = basicService.showDocument(url);

As noted in the `BasicService` demo.

In Java 6+, a call to show another web start launch file (e.g. `BasicService.showDocument(another.jnlp))` will be handed directly to JavaWS, with no browser window appearing.

I expect this has always been true of the `Desktop` API, given the JWS client is the default consumer for that file type.

Problem

I am creating a java swing application and I need to set the my program as the default program for the file extension `.mcsd` (MIME type `text/mcsd`). This must work on windows, and it would be nice if it worked on OS X/Linux as well. I am somewhat new to java (3 or 4 months) so please don't bombard me with all kinds of expert talk. I have seen associating a custom file extension with java app in windows, but I would prefer not to use the Java Web Start. If possible, I would also like to know how to set the icon of that file type. I have found exe4j as a way of creating the `.exe` and plan to use it, but if is not the best tool for convertinf to an exe or someone knows something better feel free to tell me. Another thing: I need to be able to get the data from this file after the program has opened. So my question is: how do I do this?

Original source

Related problems