Execute An External Program From Java

17 April 2007

A Java programmer may need to access the environment under which the application is running. It is supported by “ getRuntime ” method in Runtime Class. Programmer need not create an instance of Runtime class, Instead the Java application has a single instance of class Runtime environment in which the application is running.

Runtime class has several methods that are useful to execute an external program, getting Information about processors, Memory Information etc.

Following program executes an external program JavaTipsEbook.exe which is located at Local Drive.

import java.io.*;
 
public class CommandExection {
 
 public CommandExection(String commandline) {
 
 	try {
 
 		String line;
 
 		Process p = Runtime.getRuntime().exec(commandline);
 
 		BufferedReader input =
 
 			new BufferedReader
 
 			(new InputStreamReader(p.getInputStream()));
 
 		while ((line = input.readLine()) != null) {
 
 			System.out.println(line);
 
 		}
 
 		input.close();
 
 	}
 
 	catch (Exception err) {
 
 		err.printStackTrace();
 
 	}
 
 }	public static void main(String argv[]) {
 
 	new CommandExection("c:JavaTipsEbook.exe");
 
 }
 
}

Code Explanation: Here “ exec ” method of Runtime class is used, Which takes a string as an argument and instructs underlying environment to run the command. User can run any command like “ mkdir ”,“ cmd ” etc. It returns an object of Process Class which is processed using BufferedReader to show the result returned by underlying run time environment .

Runtime Class has several other methods as well. Description of some important methods are as below.

public void exit(int status): Terminates the currently running Java Virtual Machine by initiating its shutdown sequence.

public void halt(int status): Forcibly terminates the currently running Java Virtual Machine. This method never returns normally.

public int availableProcessors(): Returns the number of processors available to the Java Virtual Machine.

public long freeMemory(): Returns the amount of free memory in the Java Virtual Machine.

public long totalMemory(): Returns the total amount of memory in the Java Virtual Machine.

public long maxMemory(): Returns the maximum amount of memory that the Java Virtual Machine will attempt to use.

There are a lot of other useful methods too. For that refer Java API For Runtime Class.

del.icio.us:Execute An External Program From Java  digg:Execute An External Program From Java  spurl:Execute An External Program From Java  wists:Execute An External Program From Java  simpy:Execute An External Program From Java  newsvine:Execute An External Program From Java  blinklist:Execute An External Program From Java  furl:Execute An External Program From Java  reddit:Execute An External Program From Java  fark:Execute An External Program From Java  blogmarks:Execute An External Program From Java  Y!:Execute An External Program From Java  smarking:Execute An External Program From Java  magnolia:Execute An External Program From Java  segnalo:Execute An External Program From Java  gifttagging:Execute An External Program From Java

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Execute An External Program From Java

One Response to “Execute An External Program From Java”

  1. david Says:

    Note the documentation from Process (from Java 1.4 JavaDocs)

    If you execute a sub-process that generates stderr or stdout data, it could block or deadlock if you don’t read it…

    The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Leave a Reply