I have made a Text Editor in Java and I want to make it default text editor. Similar to Windows when it defaults to Notepad as the editor, and whose icon shows on all text files and double clicking those file(s) and opening them in Notepad.

I want to achieve the same task of making my text editor as the default one. Right clicking a text file and selecting my text editor as default app for opening text files does not resolve the issue as my text editor will not accept the file input that way. It's made to accept the file input only by FileChooser.

Is there any library in java to achieve that task of accepting the file input that way?

2 Answers
2

Deploy it using Java Web Start and declare an interest in the relevant file-types in the launch file.

..accept the file input only by FileChooser.

JWS provides the file name of interest to the main(String[]). We can create a File from the string and take it from there. Here is a demo. of the JNLP API FileService that declares an interest in the .zzz file format.

If you'd rather not use Java Web Start as Andrew suggested and would like to learn how Windows specifically handles default applications, here's a quick guide:

When you "Right click -> Open With..." and set something as a default application, a registry value is created in HKEY_CLASSES_ROOT with the name "somefileextension_auto_file" and a key "/shell/open/command" (so, HKEY_CLASSES_ROOT\somefileextension_auto_file\shell\Open\command). The default string value of this key is the shell command to be executed when a file of somefileextension type is double clicked. The default value created looks something like "c:\program files\somepath\test.exe" %1 where %1 will be a variable containing the full path to the file that was double clicked.

If you wanted to programmatically make your jar file the default application for a certain file extension, you would need to create the registry key at HKEY_CLASSES_ROOT\somefileextension_auto_file\shell\Open\command with a default value similar to the following:

java -jar c:\somewhere\yourJarFile.jar %1

and your program would need to detect when a command line argument has been given to it using public static void main(String[] args) where args[1] would be the path to the file that was clicked. If a command line argument was given, your program should automatically attempt to use it as an input file.

Note: This really shouldn't be done manually as Java was designed to be cross platform and as such there are cross platform solutions ( http://stackoverflow.com/tags/java-web-start/info ) but it seems the question author was interested in how Windows actually deals with default applications. Maybe i'll help another reader. Who knows.