| 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
Ranch Hand
Joined: Dec 16, 2010
Posts: 380
|
|
|
Check the api for response.setContentType().
|
"I have a mind like a steel... uh... thingy."
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1405
|
|
|
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: 32629
|
|
|
. . . 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: 1405
|
|
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: 1405
|
|
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
|
 |
 |
|
|
subject: how to open a file using default program
|
|
|