3

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?

3
  • 1
    This question is a quite operation system specific one...
    – Spaeth
    Jun 23, 2012 at 12:29
  • I Want to do that in "Windows" Only.....
    – Rohan Sharma
    Jun 23, 2012 at 12:30
  • Why a downvote? It's a normal question, although it has been asked multiple times.
    – 11684
    Jun 23, 2012 at 12:42

2 Answers 2

3

I have Made a Text Editor ... want to make it default text editor..

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.

3

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 ( https://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.

1
  • Thx Alex For Answering..
    – Rohan Sharma
    Jun 23, 2012 at 13:13

You must log in to answer this question.