what I'm asking is how to make any associated file type open my program with the corresponding data. For example, if you have a .doc file it opens Microsoft word with whatever is saved to that file. I know how to associate the file extension to my program, but it just opens the program from the location of the file. How do I make the program read the file upon start-up?
2 Answers
You make the program accept command-line arguments. When you open something from Explorer, it passes the name(s) of the item(s) you clicked on as command-line arguments. Your program must take these command-line arguments and do whatever processing is required... including opening and reading the contents.
In C/C++, these are the argc
and argv
arguments in int main(int argc, char *argv[])
. In Java, it's the args in public static void main(String[] args)
. In other languages, you'll have to look into it.
-
So I start my method using my file loading method where args[0] is the name if the file?– SquirvinJan 8, 2013 at 0:33
-
One more thing, when I open the program with an associated file type, I get a dialog error window titled "Java Virtual Machine Launcher" and it says "A Java Exception has occurred." Why is that?– SquirvinJan 8, 2013 at 0:43
Since this is Java and a desktop app., the obvious solution would be Java Web Start.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
Here is a demo. of the file services that associates the type .zzz
with the demo. Double click such a file and it will open in the app.