Link to home
Start Free TrialLog in
Avatar of Vanavah Edwards
Vanavah Edwards

asked on

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

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?
Avatar of for_yan
for_yan
Flag of United States of America image

To cal exe file you use

Runtime.getRuntime.exec("file.exe args");
Avatar of Vanavah Edwards
Vanavah Edwards

ASKER

Thank very much.  Would this also play a video file
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
I am getting the following error on that line
Unhandled exception type IOException
Here the line
Runtime.getRuntime().exec("player /Users/Videos/Test.avi");
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
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)
you need to make sure you have your player accessible on the Path or to have a ful path to your player program
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();
		}
	}
	
}

Open in new window

Attach is the code
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.
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 ?
No it doesn't
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 ?
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.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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");
did you try it on dos command line ?
did you go to dos folder which has the TestVideo.avi file?
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
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
check this about start command:
http://www.computerhope.com/issues/ch001041.htm

make sure that your Test.avi file is accessible form the folder where you run start command
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");
try what I posted with start command - it is more convenient if it works - first on dos window
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");
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.
it makes sense that you ned to put a path  - otherwise it will look for file only in the default folder
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
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\"");
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\""
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.
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
The program works with the full path of both the player and the video file.  Thank you very much.  Bye
You are always welcome.