Take notepad for example. You can open a file with notepad (right click and open with, or double clicking on the file if it is already associated with notepad) and it will display the contents of a file. I want to be able to open an xml file with the program that I am writing so that it can do some parsing and give a desired output. What sort of ways are there to do this in java? Do I have to ask for an argument to be passed into the program when it is opened? Would this be easier in C++ or some other language? If you need any further information please let me know -- this was the best way I could come up with to discribe my situation, which may be why my google searches have come up empty. Thank you in advance for your help!

- 168,117
- 40
- 217
- 433

- 430
- 1
- 5
- 10
-
is it windows? if so you just need to edit the registry. this has nothing to do with java on any os except for passing argument to the open command. – gigadot Sep 07 '12 at 14:38
2 Answers
You can associate an extension with a java jar and the file name will be passed as args[0]
It doesn't get much simpler than that. I suggest you try it with a "hello world" program. ;)

- 525,659
- 79
- 751
- 1,130
-
1if you are using an installer to install your program, the installer program might have the registry-editing capabilities to associate files. – lbalazscs Sep 07 '12 at 14:40
-
1@lbalazscs If it has a GUI, one possibility comes from Oracle. JWS. ;) – Andrew Thompson Sep 07 '12 at 14:43
display the contents of a file ..What sort of ways are there to do this in java?
Install/launch the app. using Java Web Start.
Java Web Start (JWS) is the Oracle Corporation technology used to launch rich client (Swing, AWT, SWT) desktop applications directly from a network or internet link. It offers 'one click' installation for platforms that support Java.
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 JNLP API file services (an API available to JWS apps.) that registers an interest (claims an association with) in the .zzz
file-type in the launch file. Associations can also be done programmatically using the IntegrationService
of the JNLP API.

- 1
- 1

- 168,117
- 40
- 217
- 433
-
JWS might not be feasible in all situations... Can one use the jnlp.jar from "traditional" jars? In other words, is it possible to create the associations with pure Java, without web/network? – lbalazscs Sep 07 '12 at 15:03
-
@lbalazscs *"JWS might not be feasible in all situations."* Sure, but noting.. *"if you are using an installer"* ..which you get to the client computer how? 2 tin cans and a piece of string? Some computers don't have Java (or the user permissions to install it), some don't have net connections, some don't have disk drives.. Not one installer or deployment approach works for every client or every app. But JWS over a network gives good coverage (& lots of extras). – Andrew Thompson Sep 07 '12 at 15:10
-
Andrew: The problems might not be technological, but organizational, psychological or economical... An installer is a solution that is familiar to everyone. Anyway, you didn't answer my question. It isn't a rhetorical one, I would like to know whether one can create the associations in pure Java (possibly using jnlp.jar) without JWS. – lbalazscs Sep 07 '12 at 15:40
-
*"..can create the associations in pure Java (possibly using jnlp.jar) without JWS."* I doubt it. I've never figured how to access any of the JNLP API services from a 'naked' desktop app. Also some JRE installations might not have the JWS jar named the same as Oracle names it (to make direct access tricky). Why don't you ask that in the form of a question (as opposed to a comment)? – Andrew Thompson Sep 07 '12 at 15:47
-
Here is my question as a question: http://stackoverflow.com/questions/12322425/file-associations-in-java-using-jnlp-jar-without-full-jws – lbalazscs Sep 07 '12 at 17:04