Java Shell Execute Equivalent

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 16591 Views - Last Post: 02 August 2010 - 06:52 PM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Java Shell Execute Equivalent

Posted 01 August 2010 - 09:10 PM

Hello

I am trying to find out how to open an mp3 file in the user's default mp3 player using Java code.

In win32 I can use a function called shell execute & it will open the mp3 file using whatever application is the default mp3 player (for example Windows Media Player, Real Player etc.).

Is there a function in Java that allows me to open an mp3 file with the default mp3 player?

This code throws an error, it will only let me open an application, I cannot open any other type(shortcut, mp3, mpeg etc.).
try 
{
     Runtime run = Runtime.getRuntime();
     Process pr = run.exec( "music.mp3" ); 
     pr.waitFor();
} 
catch (InterruptedException e) 
{
     e.printStackTrace();
} 
catch (IOException e) 
{
     e.printStackTrace();
}
catch (Exception e) 
{
     e.printStackTrace();
}

This post has been edited by gretty: 01 August 2010 - 09:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Shell Execute Equivalent

#2 Cuzzie  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 72
  • View blog
  • Posts: 342
  • Joined: 16-July 10

Re: Java Shell Execute Equivalent

Posted 01 August 2010 - 10:25 PM

Check this and this out. :)
Was This Post Helpful? 0
  • +
  • -

#3 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 12:59 AM

From the links you suppld it seems I need to use a Java 3rd party library to open mp3 files? Is that correct?

But what I want to do is NOT open & play an mp3 file but call the user's default media player(ie, Windows Media Player) to open & play an mp3 file using a function.

Is there a function to do this in Java like ShellExecute() in win32?
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 01:55 AM

Desktop.getDesktop().open(...);

should do the trick. ;)
Was This Post Helpful? 0
  • +
  • -

#5 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 02:33 AM

This will also work

Process process = Runtime.getRuntime ().exec ("C:\\Windows\\notepad.exe");


Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 07:45 AM

Why would he want to launch Notepad to open an MP3? Even if you substitute Notepad for an MP3 player, how do you know you have the system's default one?
Was This Post Helpful? 0
  • +
  • -

#7 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 09:51 AM

View Postcfoley, on 02 August 2010 - 06:45 AM, said:

Why would he want to launch Notepad to open an MP3? Even if you substitute Notepad for an MP3 player, how do you know you have the system's default one?

Correct me If I am wrong, but I thought the parameter for the open() method in the Desktop class was a File?
// type File
Desktop.getDesktop().open(File file);


You would need a file path, just like Crunch had in his example, so I do not know exactly what you mean by "how do you know you have the system's default one?". Your example and Crunch's example must both have a file path either way...there is no difference that I can see.

And both file paths must be the same as well, you cannot just put "notepad.exe", you have to put the absolute path.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 10:54 AM

except the file in my example would end in .mp3 and it would open with whatever the system was configured to open mp3s with.
Was This Post Helpful? 0
  • +
  • -

#9 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 11:45 AM

View Postcfoley, on 02 August 2010 - 09:54 AM, said:

except the file in my example would end in .mp3 and it would open with whatever the system was configured to open mp3s with.

Crunch's file would end in .mp3 as well, notepad is an example if you haven't realized -.- And it would open with whatever the system was configured to do as well. So I'm just curious as to what you meant by:

"Even if you substitute Notepad for an MP3 player, how do you know you have the system's default one?"

P.S. I just tried opening an Mp3 file with your version, gave IOException with error message Unspecified error (and no it is not a matter of not finding the file as the message is different and I used the same path for both versions). It can however, open all other types of applications.
Was This Post Helpful? 0
  • +
  • -

#10 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 01:34 PM

Well I would never do it this way, but for fun you could :bigsmile:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RobotOpener {
	public static void main(String[] args) {
		String songPath = "\\Users\\Public\\Music\\Sample Music\\Kalimba.mp3".toUpperCase();
		try {
			Robot r = new Robot();
			r.setAutoDelay(1000);
			r.keyPress(KeyEvent.VK_WINDOWS);
			r.keyPress(KeyEvent.VK_R);
			r.setAutoDelay(0);
			r.keyRelease(KeyEvent.VK_WINDOWS);
			for (int i = 0; i < songPath.length(); i++) {
				r.keyPress((int)songPath.charAt(i));
			}
			r.keyPress(KeyEvent.VK_ENTER);
		} catch (AWTException e) {
			System.out.println("ERROR, KILL ALL HUMANS!");
		}
	}
}



note as far as I know this will only work on windows, but i have not tested it other places
Was This Post Helpful? 2
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 05:56 PM

You can always

String filename = "MyMp3.mp3";
String command = "C:/Program Files/ProgramUsedToPlayMp3 " + filename;
Runtime.getRuntime ().exec (command);
Was This Post Helpful? 0
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 06:22 PM

bcranger, the method I used works well on my system. Here's my test code:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;


public class PlayMp3 {

	public static void main(String[] args) throws IOException {
		File awesomeSong = new File("/home/chris/Desktop/Deathstars/Blitzkrieg Boom.mp3");
		Desktop.getDesktop().open(awesomeSong);
	}

}


An IOException is thrown if nothing is registered to open the filetype or if the application failed to launch. Would you mind posting your code and error message. One posibility is that Windows (IIRC) is configured to play mp3, not open them.

The problem with crunch's example is that you have to specify the application. At least on my system, telling it to exec an MP3 results in an error message saying the file is not executable. The OP was specifically after something to open the file with the default app.
Was This Post Helpful? 0
  • +
  • -

#13 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 06:32 PM

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class ApplicationOpener
{
  
  public static void main(String[] args) throws IOException
  {
    Desktop.getDesktop().open(new File("C:\\Users\\Desktop\\lol.mp3"));
  }
  
}



Now when I try to open DrJava, the IDE I use by replacing with "DrJava.jar" it works fine, but doesn't seem to play MP3s. Opens folders too.
Was This Post Helpful? 0
  • +
  • -

#14 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 06:37 PM

Interesting. What does your MP3 right click menu look like? Does it have an open action? What happens if you register one?
Was This Post Helpful? 0
  • +
  • -

#15 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Java Shell Execute Equivalent

Posted 02 August 2010 - 06:41 PM

IDK I'm on Windows 7 atm...

Posted Image

Edit: Image wasn't showing, fixed link I believe.

This post has been edited by bcranger: 02 August 2010 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2