1
C:\Users\Admin\Downloads\VID_20160226_203631957.mp4

when I execute above line in command prompt the corresponding video gets played with default media player.

But when I try to do same using java Runtime class it doesnt work. I am using following method.

Runtime r= Runtime.getRuntime();
r.exec("C:\Users\Admin\Downloads\VID_20160226_203631957.mp4")
3
  • 1
    in Windows try to use command start C:\Users\Admin\Downloads\VID_20160226_203631957.mp4. If filename contains spaces use double quotes for your file path. For example: if your file name is my file.mp4 then use the command: start "my file.mp4" Commented Apr 19, 2016 at 3:34
  • 1
    Do not use runtime exec. Use Process Builder docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html Commented Apr 19, 2016 at 3:36
  • @Hector You should create an answer for that. It's the correct solution. Commented Apr 19, 2016 at 3:50

2 Answers 2

3

Use Desktop.open(File) which launches the associated application to open the file. Something like,

File f = new File("C:/Users/Admin/Downloads/VID_20160226_203631957.mp4");
try {
    Desktop.getDesktop().open(f);
} catch (IOException e) {
    e.printStackTrace();
}

You might prefer to build the path relative to the user's home directory; something like

File downloads = new File(System.getProperty("user.home"), "Downloads");
File f = new File(downloads, "VID_20160226_203631957.mp4");
Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

Runtime r= Runtime.getRuntime();
r.exec("cmd /c C:\\Users\\Admin\\Downloads\\VID_20160226_203631957.mp4");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.