aspose file tools*
The moose likes Java in General and the fly likes file type association with my java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "file type association with my java program" Watch "file type association with my java program" New topic
Author

file type association with my java program

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
hi there.
i've created my own notepad application using java.
now, i want all files with .txt extension use my program as default viewer/editor.

is there a way i can programmatically associate .txt extension with my app?

thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Only by modifying the registry (Windows) or the appropriate configuration files (Linux, Mac OS) from your program.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
so this means that it cant be done programatically?

thanks for this info mr. rob.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13870
    
  10

Jhedzkie Skies wrote:so this means that it cant be done programatically?

That's not what Rob wrote...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

It can be done, but it's going to require a bit of extra work. First of all, you need to know where and how the file association is stored. Then you need to know how to create / modify associations. And you may also want to have an undo function that allows you to restore the previous file association, in case someone wants to uninstall your application.
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
i see. i think i'll try to learn about associations first. thanks for the tip.
Tushar Kapila
Ranch Hand

Joined: Dec 23, 2007
Posts: 35

if its windows - this is how i would do this

Have a customized installer.

when installing ask user if you want to associate .txt with your program. to start with you can make a new extn like .t1 and use that

most installers have the API to call to associate an extension with a program - some of the freeware ones too.

Your program will have to a .bat to start with
which will call java command line and pass the text file name to it

java.exe com.your_package.your_main_class %*

for this to work java should be installed and be in windows PATH var

else need to add the java home path ...
c:\java_home_path\bin\java.exe com.your_package.your_main_class %*

one level deeper would be to make a custom java launcher exe but that will be in c++ - check where java is installed and then launch it with correct params


http://thehungersite.com | http://www.worldcommunitygrid.org/
 
jQuery in Action, 2nd edition
 
subject: file type association with my java program
 
Similar Threads
access .txt files under the particlar parent directory
JFileChooser showing files and directory
add timestamp with .txt file & write some information to the .txt file
Checking File Type
Java program that changes a file's extension