aspose file tools*
The moose likes Java in General and the fly likes how to open a file using default program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to open a file using default program" Watch "how to open a file using default program" New topic
Author

how to open a file using default program

jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
Hello Ranchers
here to disturb you guys again
can any one please tell me how can i open any file using default program
like example : text file with notepad or mp3 file with windows media player or any other player which is my default program or like starting my default browser
or if i just type like for example media player it should start default player
please help
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 917
    
  13

Check the api for response.setContentType().


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

You could use java.lang.Runtime to call the Windows command interpreter cmd.exe with the /c start option and provide a path to the file you want opened using the default program, if any.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
Jelle Klap wrote:You could use java.lang.Runtime to call the Windows command interpreter cmd.exe with the /c start option and provide a path to the file you want opened using the default program, if any.

i tried doing that but m not abel to open images and audio files
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
i tried opening it with Desktop.getDesktop().open
code compiled even executed but did not played the file
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
. . . and with an empty catch block, you will never know what went wrong. At least put ert.printStackTrace(); in it, then you will have some idea what has happened.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Right, Desktop would be your best option as of Java 1.6.
I think it's very likely you're swallowing some kind of exception in your current program.

Edit: Tooooooo sloooooooow.
jaspreet manchinda
Ranch Hand

Joined: Feb 27, 2012
Posts: 64
Campbell Ritchie wrote: . . . and with an empty catch block, you will never know what went wrong. At least put ert.printStackTrace(); in it, then you will have some idea what has happened.

i forgot to do that well its giving
java.io.IOException: Failed to open file: .mp3. Error message: Unspecified error

at sun.awt.windows.WDesktopPeer.ShellExecute(Unknown Source)
at sun.awt.windows.WDesktopPeer.open(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at OpenFolderInJava.main(OpenFolderInJava.java:25)
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

Dang, I would've bet my house on a FileNotfoundException
Try opening the file using a canonical path as stated in this bug database entry: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6385002

 
jQuery in Action, 2nd edition
 
subject: how to open a file using default program