When you double click the icon of an MS Word document (.doc files), or a Photoshop file (.psd), etc., the suitable program to read the file is called by the OS.
I’m working on an interpreter that reads binary instructions and executes them. I can easily add the option to import files from disk to the program, but how can I make the program be able to open a file when called by the OS (like in the examples above)?
2
Answers
You can of course do this in Windows explorer by right clicking and select “Open With.” It then passes the file as the first argument to your application. If that was all you know, then all of the following will be unnecessary to you. 🙂 But I think you want to know how to write code to do this for end-users, so here it is:
I don’t know of an API to do this, so I believe you need to add the appropriate registry keys. Suppose your file extension is .prog. Since you mentioned MS Word I assume you are doing this on Windows.
You can see an example of this if you go into the registry and pick any existing extension. Take “.txt” There’s other keys in there too, that can tell the OS the mime type of the file, and how to create a new one via right-clicks, and how to embed that type inside a Word Document. I’m guessing you won’t need any of that.
Once you do this, you can export a .reg file with the appropriate stuff in it. When someone runs your app, you can write code to create these registry keys and point them to wherever your EXE is installed. That last part is specific to Windows so you will have to look into how to create registry keys using Java, or you will have to defer it to your Windows installer.
Now, when you run your application, Windows will run “MyProgram.exe FULL_PATH_TO_THE_FILE”. So you will need to parse the command-line to get the file name and automatically open it.
UPDATE: Since this is Java you won’t have an EXE. That’s okay: I think you run a .jar file by doing something like “java.exe -jar MyProgram.jar” So in your case, the registry key will need to be “java.exe -jar C:Program FilesMyProgram.jar %1” which will do the same thing.
You need to register the file type (the “open” verb) as calling a program like
java.exe -jar yourjar.jar "%1"
. And then the Main java class is started, and the main method gets the filename(s) as a start arguments array.You can then display or edit it, however you might open it in an already running application instead. You cannot easily* use the DDE mechanism for passing a new filename to an open Java window (but you can code that with al alternative inter process mechanism between the two Java instances).
In the registry it would be something like
HKLMSOFTWAREClassesyourextshellopencommand=java.exe -jar C:yourjar.jar "%1"
The MSDN has some good information on the general mechanism. Look for registering verbs on http://msdn.microsoft.com/en-us/library/windows/desktop/dd758090%28v=vs.85%29.aspx (asuming you are talking about Windows, of course).
)* Java Web Start actually allows to define file handlers and to run with a SingleInstanceService
BTW: in this answer somebody suggests it can be done with install4j: listen for "open file with my java application" event on windows