On Windows you can't easily associate file extensions with Java applications. However, there is a workaround!

In short the reason is that you can't associate file extensions to trigger any other file on Windows. You can only choose to execute a limited set of extensions. Which is: .exe, .pif, .com, .bat, .cmd.

An easy solution, would be to write a Batch (.bat) file. Thereby when the custom file extension is double-clicked, then it executes a Batch file, which then executes the Java application.

For this tutorial, the file extension .awesome will be used. Feel free to choose any other file extension, and adapt as needed.

Test Application

The filename of the double-clicked .awesome file, will be included in args in main. If the Java application was executed without double-clicking a .awesome file, then no additional filename will be included.

public static void main(String[] args)
//                               ^ args[0] is the .awesome filename

The following Java example, will be used for testing throughout the tutorial. It simply opens a message dialog, showing the command-line arguments.

Export the Java application as Awesome.jar.

import javax.swing.JOptionPane;

public class AssociatedFileExtension {
    public static void main(String[] args) {
        String text = "Count: " + args.length + "\n";

        for (int i = 0; i < args.length; i++) {
            text += args[i] + "\n";
        }

        JOptionPane.showMessageDialog(null, text, "Awesome", JOptionPane.INFORMATION_MESSAGE);
    }
}

Batch File

The following Batch file must be saved in the same directory as Awesome.jar, with the name Awesome.bat.

@echo off
title Awesome
start javaw -jar "%~dp0Awesome.jar" %1 %~dp0

Explanation:

Setting the title or turning off command prompting, doesn't actually matter as the console is only visible for a fraction of a second.

Last but not least

Before we're done, we need to actually assign the .awesome file extension, to our .bat file.

  1. Right click on any file having the .awesome file extension and press "Open with...".
  2. Now, browse for a program and select the .bat file previously created.
  3. Remember to "check" (✓) the "Use this application for all .awesome files".

If the .bat file is moved, then these previous steps needs to be redone. Also remember that this needs to be done every time the program gets onto a new computer.

This can somewhat be automated further, by creating a .reg file. Though this needs to be executed as well. Which could be done in an installation process.

This could probably also be automated in the Java application, making it automatically do it upon first launching it. Though this might hit some compatibility issues, for older Windows versions.

I might add examples later, but for now here is some resources:

Done

Now, create a file with the .awesome extension, and double-clicking it, should look something like this:


This tutorial was inspired by an answer I wrote on Stack Overflow, half a year ago.


Windows cannot find 'javaw'

If you are getting this error. Then don't worry it's a pretty common error and easy to fix.

The cause of the error, is that Windows can't find Java in its PATH environment variable.

First find where Java is installed on the computer and copy the path. It should look something like this (depending on your Java version and the drive you installed it on):

- Java 1.6.X -> "C:\Program Files (x86)\Java\jre6\bin"
- Java 1.7.X -> "C:\Program Files (x86)\Java\jre7\bin"
- Java 1.8.X -> "C:\Program Files (x86)\Java\jre8\bin"

Then the path to Java needs to be added to the PATH environment variable:

  1. Go to PC Info (3 ways)
    • Right click on "My Computer" or "This PC" and select "Properties".
    • Go to the "Charms Bar" press "Settings" and then "PC Info" (Windows 8).
    • Simply paste "Control Panel\All Control Panel Items\System" into File Explorer.
  2. Now click on "Advanced system settings"
  3. Now in "System Properties" click on the "Environment Variables" button.
  4. Now in the "System variables" section, scroll down and find a value called PATH (doesn't have to be all uppercase).
  5. Select it and press "Edit".
  6. Then in "Variable value" add a semicolon ";" at the end and after that add the path to Java.

If there aren't a variable called PATH, then:

  1. Then press "New".
  2. In "Variable name" write PATH.
  3. In "Variable value" write the path to Java.