The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


File Associations -- Programmatically

Posted by Matt Gerrans on January 22, 2002 at 5:15 PM

> But how do I get my program to change the file associations? Because I would want to change the file associations on customers' computers so that when they double click on my JAR files, they open up like an exe.

Ah ha. You want to do it programmatically. Well here is one relatively simple way:

First, create a text file, jar.reg, with content simlar to this (of course, customize the display name ("@=...") and java.exe location to your liking):



@="Executable Jar File"



@="C:\\Program Files\\JavaSoft\\JRE\\1.3.1\\bin\\javaw.exe -jar %1"

(You could change the %1 to %L if there are short/long filename problems. Also, although you see both '\' and '\\' used above, rest assured it is correct.)

Now all you have to do is import that reg file; if you do a Runtime.exec() on "RegEdit /s \jar.reg", then it will be imported silently into the Windows registry. Without the "/s", you will get a silly little messagebox telling you it was imported. You should specify the complete path to your reg file.

You can dynamically create the reg file in the temp directory and also delete it when you are finished, by the way.

Of course, there are a myriad of other methods, such as doing it with a little Windows Script Host script (JScript, or VBScript (ick!)), using an INF file (if you don't already know how, this is complicated and ugly, too), writing a Java Native Method for registry manipulation (overkill, unless you need it for other purposes) or writing a little C/C++ tool to do it (kind of redundant, since that is exactly what RegEdit is) and so on.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us