7

I need to be able to run a .mp3 file using Java, I have tried this, but to no avail:

Process process = new ProcessBuilder("C:\\Users\\<removed>\\Desktop\\Music\\Cash Cash\\Overtime.mp3")

and then running

process.start();

But, it throws this error:

java.io.IOException: Cannot run program "C:\Users\<removed>\Desktop\Music\Cash Cash\Overtime.mp3": CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessBuilder.start(Unknown Source)
    at com.newgarbo.music.Mooseec.main(Mooseec.java:50)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 2 more

I assume this is of course because a Process is only for executables/jars, and if it is so, then can someone please show me a way to run these files? ^_^

janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    A `.mp3` file is not a program. It's a file that requires a program to interpret it's contents, and play them over your speaker. You will need something like: http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java#answer-10237397 – SnakeDoc Dec 15 '14 at 21:06
  • 1
    `Overtime.mp3` is not an executable. You must run an executable associated with MP3 and just pass the MP3 file to the running process. – Lyubomyr Shaydariv Dec 15 '14 at 21:06
  • If you want to run it like that and just run it using default program just use cmd (shell). It will open it as default for you. – Ya Wang Dec 15 '14 at 21:08

2 Answers2

8

You could use Desktop.open(File) to launch the associated application to open the file. Something like,

File mp3 = new File("C:\\Users\\<removed>\\Desktop\\"
    + "Music\\Cash Cash\\Overtime.mp3");
Desktop.getDesktop().open(mp3);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 2
    @bernhardkiv You're welcome. Not that you asked, but the JavaFX [`MediaPlayer`](http://docs.oracle.com/javafx/2/api/javafx/scene/media/MediaPlayer.html) should also allow you to play it with pure Java code. – Elliott Frisch Dec 15 '14 at 23:03
  • I will look into it, that may be more useful than just running it :) –  Dec 16 '14 at 14:41
0

I've never had much luck with relying on the Windows file associations to launch files. Two options come to mind:

  1. Use wmplayer.exe
  2. Use vlc.exe

wmplayer.exe should be included with most Windows installations (post Vista) and can be run using the following:

String command = "C:\\Program Files (x86)\\Windows Media Player\\wmplayer.exe";
String argument = "C:\\Users\\<removed>\\Desktop\\Music\\Cash Cash\\Overtime.mp3";
Process process = new ProcessBuilder(command, argument).start();

If you wanted to be consistent and not rely on anything that may or may not be installed you could bundle vlc with your application and use it instead. The process to launch it is identical to the above, just the path for the command would need to change.

Foosh
  • 1,195
  • 12
  • 16