Introduction
In Windows, you can associate any custom command with a file extension.
For example, if you want Windows to automatically run Java .jar
files using the command java -jar
. These examples will walk through the steps necessary to do this:
- Update the
PATHEXT
environment variable - Associate the file extension to a file type
- Specify a command to run when that file type is executed
This example will focus on using java
to run a .jar
file, but this example works just as good for any custom application, or creating script runners for Python and Ruby.
Steps
You must run all these commands from an admin command prompt.
To get an admin command prompt right click on the command prompt and chose Run as administrator
.
Update the PATHEXT environment variable
The first step is to update the PATHEXT
environment variable.
Add .jar
or whatever to your PATHEXT
using the built-in command setx.
setx PATHEXT %PATHEXT%;.JAR
To learn more about setting environment variables in Windows, check out my tutorial Set Environment Variables in Windows.
Associate the file type to a command
These commands will associate the file extension to a custom launcher command.
assoc .jar=JavaJar.File
ftype JavaJar.File=%JAVA_HOME%\bin\java.exe -jar "%1" %*
The first command assoc associates the file extension with a file type registered with Windows. The second command ftype ties a custom command to the file type.
The command that runs is %JAVA_HOME%\bin\java.exe -jar "%1" %*
. It uses the JAVA_HOME
environment variable to always pick the configured Java version. The %1
represents the first argument provided (the file being executed) and %*
represents all other arguments passed (if any).
Things to remember
- If you need a more than one-line to write a custom launcher, put it in to a
.bat
or.cmd
file and then use that as your launcher. - You can usually omit the file extension when running the file. For example, using the example above would let you run a Java JAR file named
TcpNull-1.0.jar
by simply typing in to the command-promptTcpNull-1.0
. This assumes the JAR file lives in a directory in the PATH environment variable. If you need to learn how to set environment variables check out my tutorial Set Environment Variables in Windows. If you want a JAR file to test with you can download my simple TCP Null application. - If you are using a launcher for a programming language like Java, Python, or Ruby, there are two versions of the launcher. For example:
java
andjavaw
,python
andpythonw
, andruby
andrubyw
. The version with thew
is meant for Windowed applications that have a GUI and will not open a command prompt.
Running the file
To "run the file" and execute the command you associated with the file type, you simply need to type the file name in the command prompt or double click on the file from your Windows file explorer.
You can run commands a few ways:
- from the command-prompt
- from powershell
- in a Batch script (.bat)
- from the Run window (WindowsKey+R)
These would all be equivalent from the command-prompt
# The full command you would have to run before
# in the example above
java -jar myapp.jar
# Calling the file directly by name
# Assumes file is in the same dir or in the PATH
myapp.jar
# You can usually omit the extension too (except run window)
myapp
Again, you can also just double click the file from your Windows file explorer once you have the associations complete.
Conclusion
After following this guide you should understand how to create a custom launcher for any file type. This allows you to run files directly in the command-prompt or double-click them in the Windows explorer.