• Post Reply Bookmark Topic Watch Topic
  • New Topic

Open file using default editor  RSS feed

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hell friends, sir , madam

I have a problem.
Consider a simple java program wherein I get the file path as input. Based on the extension of file I want to launch the associated editor.
Example if I give a path of .jpg file then the file should get opened in the editor installed on the machine.
I tried using Runtime class which has exec method.
I want to identify which is the default editor to open a file given the file extension. How do I do that ? Are there any entries in registry wherein I get what editor is to be launched when you have so n so file extension ?

I will be thankful for any help.
Waiting for your replies.
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear sir,

i'd say that this is too os-dependent to be solvable with java...

best regards,
jan
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On the other hand if you only want to do this on operating systems where the question is meaningful (i.e. only on Windows) then using Runtime.exec() to execute "cmd /c start yourfilenamegoeshere.ext" will work.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, easy integration with native applications is a feature for Java 6.0 and up and comming.

http://java.sun.com/developer/JDCTechTips/2005/tt0505.html
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul's "cmd" trick is a good one. You can get the same effect with RUNDLL32 ... Google for "java rundll32" for more examples. I'm not sure how you'd choose cmd over rundll or vice versa.

You can test this by just typing a filename into a command window. Try a few extensions and see what happens. My guess is that this uses the very same extension mappings that Windows Explorer uses to open files when you doubleclick.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!