File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Executing files....mp3 style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Executing files....mp3 style" Watch "Executing files....mp3 style" New topic

Executing files....mp3 style


Joined: Feb 15, 2002
Posts: 1
Hello everyone.
I am new to this, so take it easy on me
I was trying to learn how to mess with files, and I decided to write a program that will search a specified directory for a specified file extension. so if i wanted to search C:\!files\ for .mp3 files, it returns a printed list of the files with that extension. well, now that i can do that, what do i do with the files? i have winamp on my computer, and i would like to play these mp3 files like a playlist, but have no way to go about executing an mp3 file, or even opening winamp with java. anyone have any ideas? what classes/methods/etc i should use?
public class Search {
public static void main(String args[]) throws {

// create a buffered reader "in" to store a string that you input
// from the keyboard.
BufferedReader in =
new BufferedReader(new InputStreamReader(;
System.out.println("Enter the directory for search...");

// reading in the string typed from the keyboard, and stored as String sBlah
String sBlah = in.readLine();

System.out.println("String in = " + sBlah);
File dir = new File(sBlah);
System.out.println("dir.isDirectory()=" + dir.isDirectory());
if(dir.isDirectory() == true) {
System.out.println("Enter the file extension you would like to search for.");
System.out.println("Avoid * and quotations.");
final String sExtension = in.readLine();
String[] sPlayList = dir.list(new FilenameFilter() {
public boolean accept(File f, String s)
} // end accept

System.out.println("Listing files....");
for (int i = 0; i < sPlayList.length; i++)
} // end for
if(sPlayList.length == 0) {
System.out.println("There aren't any Mp3's in this directory dummy!");
} // end if
} // end if
System.out.println("The directory that you entered does not exist...Typical..");
} // end else

} // end main
} // end class Search

Mike O'Brien
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Well, have a look at the methods exec() in class java.lang.Runtime which allow you to start a new process from a Java application. You can build a command line with "winamp" in it and some MP3 files as arguments and then feed it as arguments to exec()... Might be worth trying...
Moreover, we'd like you to read the Javaranch Naming Policy and modify your "Publicly Displayed Name". Thank you for your collaboration.
[ February 15, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
Michael O'Brien

Joined: Feb 15, 2002
Posts: 2
Thanks for the help, sorry about the nick name, I am now known as Michael O'Brien.
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
Try this.
public class Excute {
public static void main(String args[])throws Exception{
Runtime run =Runtime.getRuntime();


Sharing is learning
Michael O'Brien

Joined: Feb 15, 2002
Posts: 2
I ended up doing something like this....
for (int i = 0; i < sPlayList.length; i++)
String sTrack = (sBlah + sPlayList[i]);
String sWinPlay = "C:\\Program Files\\Winamp\\winamp.exe " + sTrack;
Process proc = Runtime.getRuntime().exec(sWinPlay);
int rc = proc.exitValue();
System.out.println("rc=" + rc);
} // end try
catch(InterruptedException e)
System.out.println("InterruptedException caught for Windows Media Player: " +
} // end catch
catch(IOException e)
{ {
System.out.println("IOException caught for Windows Media Player: " +
} // end catch
} // end for
but now, it will loop through, and it will call each song from the PlayList[] array, and pass the song name into the string winplay as a parameter, and everything works fine, except it waits for the entire media player .exe to close before it calls the next one. what can i do to get windows media player to exit/terminate/close/etc?
I agree. Here's the link:
subject: Executing files....mp3 style
It's not a secret anymore!