Open a file in the default application using the Windows command line (without JDIC) / by Frank Worsley

Quite a few people have asked me about this in the past. If you have a file how can you open it in the default associated application without querying the registry or using some other Windows API? Or if you program in Java how can you do it without using JDIC?

The easiest way to do this is using the "start" command. For example to open the file "readme.txt" in the default text editor you would do this:

C:\>start readme.txt

You can also use start to open folders or follow shortcuts:

C:\>start "My Shortcut" // note that you don't need .lnk at the end

This will open the target of the "My Shortcut" shortcut. If the shortcut points to a folder it will open a Windows Explorer window for it, if the shortcut points to a document it will open it in the default application and if the shortcut is for a program it will launch the program.

The trick is that "start" isn't an executable. It is a built-in command of the Windows command line interpreter "cmd.exe". In Java (and other languages) if you try to create a process using the "start" command this will fail since there is no "start.exe" executable in the system.

Instread you have to invoke "start" through the "cmd.exe" interpreter. This can be done using the /C flag:

cmd /c "start readme.txt"

This can be run successfully in Java using Runtime.exec() or a ProcessBuilder. Simply calling "start" directly would fail. Note that this limitation is also true for many other Windows commands. If something fails to invoke you should always try running it using "cmd /c".