• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to open a file using default program

 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1801
28
Chrome Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the api for response.setContentType().
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jaspreet manchinda
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried opening it with Desktop.getDesktop().open
code compiled even executed but did not played the file
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . 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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic