Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Run External Windows Program with Java

This article will help you to execute Windows Program like Media Player , Notepad and many other application with help of java.

It will help you execute Windows Program like Media Player , Notepad and many other application with help of java. Here we discussed 3 examples (Notepad, Media Player, Task Manager list), using these example you may make your own logic like terminating a program, obtaining a directory listing, Remote Assistance Programs etc.

Listing 1 : First we import all the necessary classes

import java.io.BufferedReader;
import java.io.InputStreamReader;

The use of these 2 imports would be made more clear when we discuss on task manager output.

Now we define our classes and the main function :

Listing 2 : Main function

public class ExternalApp {
	public static void main(String args[])
	{
		try
		{
			

Now to execute any external windows Program we make use of Runtime.getRuntime.exec(command);

Here Runtime allows the application to interface with the environment in which the application is running and the current runtime can be obtained from the getRuntime method.The exec method helps to execute a particular program given in the argument.

Lets open the windows Notepad, for this we simply need to write:

Listing 3: Opening notepad

Runtime.getRuntime().exec("notepad.exe");

Here we pass notepad.exe as the argument so that when this java program gets executed notepad will get opened.

You can even open your media player and even play a particular song on that via Java. Just you use this:

Listing 4 : Opening Windows Media Player

Runtime.getRuntime().exec("cmd /c start \"%programfiles%\\Windows Media Player\\wmplayer.exe\" \"e:\\2.mp3\"");

Here cmd /c start is used to start the program.

%programfiles%\\Windows Media Player\\wmplayer.exe is the path of media player so that it gets opened.

e:\\2.mp3 is the path where the music file is stored on the computer.

Now we move to some difficult task like we want to open a program and want to display all information from this external program. The best example would be listing all the process running in Taskmanager.

For this we code:

Listing 5 : Process Runing in Taskmanager

String line;
        Process p = Runtime.getRuntime().exec("tasklist.exe");
        BufferedReader input =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line); //<!-- Parse data here.
        }
        input.close();
Here line will store all the information obtained from task manager like Running process, CPU usage etc.
First we execute tasklist.exe which returns allthe active process running. We execute it in a process named p.
Now we obtain all the information returned from tasklist using p.getInputStream() We make a BufferedReader object to store this.
Now we read each line from this BufferedReader object which will give each active process and we display it on the console.
Finally we end our program by a catch statement to handle the exception.
}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}
Output :

img1
img2
Full Source Code :

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ExternalApp {
	public static void main(String args[])
	{
		try
		{		
			
		Runtime.getRuntime().exec("notepad.exe");
		Runtime.getRuntime().exec("cmd /c start \"%programfiles%\\Windows Media Player\\wmplayer.exe\" \"e:\\2.mp3\"");
        String line;
        Process p = Runtime.getRuntime().exec("tasklist.exe");
        BufferedReader input =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            System.out.println(line); //-- Parse data here.
        }
        input.close();
      
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}


My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login