We help IT Professionals succeed at work.

# How to play video file and run exe file from within Java

on
1,086 Views
I have video files of type avi and swf and want to play them from within my Java program.  I also have an exe that I want to call.  How do it?
Comment
Watch Question

## View Solutions Only

Awarded 2011
Awarded 2011

Commented:
To cal exe file you use

Runtime.getRuntime.exec("file.exe args");

Commented:
Thank very much.  Would this also play a video file
Awarded 2011
Awarded 2011

Commented:
So if you have executable which plays video file like
player.exe filename.avi

Then yoiu do ot this way from java:

Runtime.getRuntime(). exec("player filename.avi");

Check the API for Runtime and Process classes for details

Commented:
I am getting the following error on that line
Unhandled exception type IOException

Commented:
Here the line
Runtime.getRuntime().exec("player /Users/Videos/Test.avi");

Commented:
Okay I am using a try/catch and that takes care of the problem.  However, it would not run the file without a player.  On its own the file runs.  Is there an argument you can add to enable it to run automatically by using the default player.  I don't want to put in the path for the media play as every operating system or computer is different

Commented:
I forget the error I am getting is now

java.io.IOException: Cannot run program "Test.avi": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
Awarded 2011
Awarded 2011

Commented:
you need to make sure you have your player accessible on the Path or to have a ful path to your player program

Commented:
import java.io.IOException;

public class RunVideoExample {

public static void main(String[] args) {
// Code to run executable file
//		Runtime.getRuntime().exec("file.exe args");
try {
Runtime.getRuntime().exec("TestVideo.avi");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Attach is the code

Commented:
i agree with you.  My laptop automatically runs any video file from a double or the cd/dvd drive.  So I am at a lost.
Awarded 2011
Awarded 2011

Commented:
You cannot do that:
Runtime.getRuntime().exec("TestVideo.avi");

you need to indetify the execiutable which really plays, like player.exe or something
and have

player TestVideo.avi

First open dos window and type there

player TestVideo.avi
does it play without any java ?

Commented:
No it doesn't
Awarded 2011
Awarded 2011

Commented:
well because it is not called player.exe and because you need to know where it is located - it is not in your path

I think windows media player is called

wmplayer.exe

buty I still don't kow in what folder it is sitting

Do you have windows media player insattled on your machine ?

Commented:
I understand what you mean.  This is okay for my laptop.  But if I got to put this on other systems where people download this java program.  It has to run the video automatically similar to how it runs when you double click on a video file.
Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
Awarded 2011
Awarded 2011

Commented:
well you may assume that they all have windows media palyer and installed in the same place, and write it in the instruction to your program
Chances are that most of the users will have it and installed in the same place

Commented:
The code you posted runs wmplayer only.  It doesn't run the file

Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe /res/img/TestVideo.avi");
Awarded 2011
Awarded 2011

Commented:
did you try it on dos command line ?
did you go to dos folder which has the TestVideo.avi file?
Awarded 2011
Awarded 2011

Commented:
check this - maybe you need to have /play  and /close
switches on the command line:
http://support.microsoft.com/kb/241422

first do everytthing in dos woindow - when you see it plays in dos window then go to java
Awarded 2011
Awarded 2011

Commented:
id you have association in windows with the avi extension you can try to use
start command

like

start TestFile.avi

again type on the dos command line first - if it works then go to java
Awarded 2011
Awarded 2011

Commented:
http://www.computerhope.com/issues/ch001041.htm

make sure that your Test.avi file is accessible form the folder where you run start command

Commented:
The full command runs video through wmplayer in dos.  The wmplayer ONLY runs from Java.  I have the video file in the same folder as the java class.  But it would not open the video file.
Here is the code.  It only opens wmplayer.

Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe TestVideo.avi");
Awarded 2011
Awarded 2011

Commented:
try what I posted with start command - it is more convenient if it works - first on dos window

Commented:
I tried it with the start command.  I put the video file in the root directory and it works.  But i had to put in the path of the video file

Runtime.getRuntime().exec("\program files\windows media player\wmplayer.exe C:\TestVideo.avi");
Awarded 2011
Awarded 2011

Commented:
yes, I tried in the dos window

start sine_wave.avi

works for me

Try to put the same in java:

Runtime.getRuntime().exec("start c:\\folder\\path\\to\\avi\\file\\sine_wave.avi");

make sure that your path to the file does not have spaces inside.
Awarded 2011
Awarded 2011

Commented:
it makes sense that you ned to put a path  - otherwise it will look for file only in the default folder

Commented:
The same command works in Java.  It runs the avi file from the root folder but it wouldn't run it from my workspace in my documents folder even though i have the file in the same folder as the Java Class
Awarded 2011
Awarded 2011

Commented:
because you have spaces in the path

with spaces you need to do smething like tht:

Runtime.getRuntime().exec("start \"c:\\folder with space\\path with space\\to\\avi\\file\\sine_wave.avi\"");
Awarded 2011
Awarded 2011

Commented:
on the ommand line if youtr path has spaces you need to cnsloe your argument in double quotes
in java in order to show double quotes within quotes you need to escpe internals quotes
with backshlash in front of it  like that
"insde quote \"this is inside\""

Commented:
let me explain in a different way.  This command runs the video file in player from the root directory -->>
Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe C:/TestVideo.avi");

But I want to run the same file from my Eclipse workspace which is in myDocuments folder.  The same avi file is in the same folder as my Java class program.  So the code I use to run the same file from my workspace folder is --->>
Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe TestVideo.avi");
But it wouldn't run the avi file only the player.  So I think it is a path problem I cannot resolve.

Commented:
SORRY I MEANT:
From in my Java class, this command runs the video file (which is located in the root folder) in wmplayer -->>
Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe C:/TestVideo.avi");

But I want to run the same file from my Eclipse workspace which is in myDocuments folder.  The same avi file is in the same folder as my Java class program.  So the code I use to run the same file from my workspace folder is --->>
Runtime.getRuntime().exec("/program files/windows media player/wmplayer.exe TestVideo.avi");
But it wouldn't run the avi file only the player.  So I think it is a path problem I cannot resolve.
Awarded 2011
Awarded 2011
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
Awarded 2011
Awarded 2011

Commented:
it does not matter in the same with your java class or not - you need to have the full path and with spaces you need to put the path in double quoes, and better use start as it will make it independent from whetre your player is installed and use  windows notations inside your exec command - back slashes and C:, etc.

Commented:
The program works with the full path of both the player and the video file.  Thank you very much.  Bye
Awarded 2011
Awarded 2011

Commented:
You are always welcome.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.