Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a file using Java? [duplicate]

Tags:

java

audio

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? ^_^

like image 367
bernhardkiv Avatar asked Dec 15 '14 21:12

bernhardkiv


1 Answers

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);
like image 89
Elliott Frisch Avatar answered Oct 17 '22 00:10

Elliott Frisch