Java - Execute System Commands

This article describes how to execute System Commands using Java program.

The Runtime class allows the Java application to interface with the system environment. Using this Runtime class, the system commands can be executed using the api Runtime.exec().

Consider the below example.

ExecuteSystemCommands Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

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

/**
 * This class executes the System commands using Runtime class
 */

public class ExecuteSystemCommands
{
        public static void main(String args[])
        {
                try
                {
                        //Get the System Runtime
                        Runtime runtimeInstance = Runtime.getRuntime();
                       
                        //Execute the command "help" using the exec() method
                        Process p = runtimeInstance.exec("cmd /c help");
                       
                        //Get the Input stream and read the response or output given by the "help" command
                        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                        String line = reader.readLine();
                        while (line != null)
                        {
                                System.out.println(line);
                                line = reader.readLine();
                        }
                }
                catch (Exception e)
                {
                        System.err.println("Error in executing the System command");
                        e.printStackTrace();
                }
        }
}

Output:

 For more information on a specific command, type HELP command-name
ASSOC          Displays or modifies file extension associations.
ATTRIB         Displays or changes file attributes.
BREAK          Sets or clears extended CTRL+C checking.
BCDEDIT        Sets properties in boot database to control boot loading.
CACLS          Displays or modifies access control lists (ACLs) of files.
CALL           Calls one batch program from another.
....
....
....
For more information on tools see the command-line reference in the online help.

Execute Batch file?
Windows Batch file can be executed using the Runtime.exec() method as below.

String cmdArray[] = { "cmd.exe", "/C","START Call conf/batch.bat"};
Process p = Runtime.getRuntime().exec( cmdArray );
//Get the Input stream and read the response or output given by the batch file
Technology: 

Search