How to run a file using Java?

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

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

and then run

 process.start(); 

But this causes 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 guess this is, of course, because Process is only for / jars executables, and if so, can someone show me a way to run these files? ^ _ ^

+7
java audio
source share
2 answers

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

 File mp3 = new File("C:\\Users\\<removed>\\Desktop\\" + "Music\\Cash Cash\\Overtime.mp3"); Desktop.getDesktop().open(mp3); 
+8
source share

I have never been able to rely on Windows file associations to run files. Two options come to mind:

  • Use wmplayer.exe
  • Use vlc.exe

wmplayer.exe should be included in most Windows installations (post Vista) and can be started 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 want to be consistent and not rely on anything that may or may not be installed, you can associate vlc with your application and use it instead. The process of starting it is identical to that described above, only the path for the team must change.

0
source share

All Articles