- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to execute an external program like windows media player in Java?
Using the Runtime class
Java provides a class named java.lang.Runtime, using this class you can interface with the current environment.
The getRunTime() (static) method of this class returns a Runtime object associated with the current application.
The exec() method accepts a String value representing the command to execute a process in the current environment (system) and executes it.
Therefore, to execute an external application using the Runtime class −
Get the run time object using the getRuntime() method.
Execute the required process by passing the path of it as a String value to the exec() method.
Example
import java.io.IOException; public class Trail { public static void main(String args[]) throws IOException { Runtime run = Runtime.getRuntime(); System.out.println("Executing the external program . . . . . . . ."); String file = "C:\Program Files\Windows Media Player\wmplayer.exe"; run.exec(file); } }
Output
System.out.println("Executing the external program . . . . . . . .
Using the ProcessBuilder class
Similarly, the constructor of the ProcessBuilder class accepts a variable arguments of type string representing the command to execute a process and its arguments as parameters and constructs an object.
The start() method of this class starts/executes the process(es) in the current ProcessBuilder. Therefore, to run an external program using the ProcessBuilder class −
Instantiate the ProcessBuilder class by passing the command to execute a process and arguments for it as parameters to its constructor.
Execute the process by invoking the start() method on the above created object.
Example
import java.io.IOException; public class ExternalProcess { public static void main(String args[]) throws IOException { String command = "C:\Program Files\Windows Media Player\wmplayer.exe"; String arg = "D:\sample.mp3"; //Building a process ProcessBuilder builder = new ProcessBuilder(command, arg); System.out.println("Executing the external program . . . . . . . ."); //Starting the process builder.start(); } }
Output
Executing the external program . . . . . . . .
- Related Articles
- How to use Android Media Player Singleton?
- Install VLC Media Player in Ubuntu
- Tips for vlc media player lovers
- How to compile and execute C# programs on Windows?
- Resetting a Root Password in Linux without External Media
- Execute a script when media data is loaded in HTML?
- How to add an audio player to an HTML webpage?
- Execute a script when the media is ready to start playing in HTML?
- How to embed youtube as an audio player?
- Execute a script when the media has started playing in HTML?
- How to write JavaScript in an External File?
- How to set JAVA_HOME for Java in Windows?
- Execute a script when the length of the media changes in HTML?
- How to read an external JSON file in JavaScript
- How to resolve javac is not recognized as an internal or external command in java?
