Java - Execute Shell Command Using Runtime.exec

By Softorks | October 17, 2019 | Updated : October 17, 2019

English | French

1. Description

Runtime.exec (String command) - This function provides the ability to execute the command provided in the arguments in a separate process.

2. Declaration


    public Process exec(String command)
                    

3. Parameters

command - the command to be executed

4. Return value

This method returns a process object

5. Implementation

In this example, we will execute the ls -l / command to list all the directories present in the home folder and display their permissions.

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

public class ExecuteShellCommandRuntimeExec {
	public static void main (String [] args) {
		try {
			Process process = Runtime.getRuntime().exec("ls -l"); 
			
			StringBuilder output = new StringBuilder(); 
			
			BufferedReader reader = new BufferedReader(new InputStreamReader (process.getInputStream()));
			
			String line;
			
			while((line = reader.readLine()) != null) {
				output.append(line + "\n");
			}
			
			int exitVal = process.waitFor();
			if (exitVal == 0) {
				System.out.println("Success");
				System.out.println(output);
				System.exit(0);
			} else {
				System.out.println("Something abnormal has haapened :( ");
			}
				
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
                       

Output:

vpn-133-230-1-200:~ softorks$ ls -l /
total 13
drwxrwxr-x+  81 root  admin  2592 Oct 17 11:02 Applications
drwxr-xr-x+  69 root  wheel  2208 Aug 21 12:47 Library
drwxr-xr-x    3 root  wheel    96 Oct 17 10:44 Network
drwxrwx---  203 root  Virex  6496 Oct 15 19:37 Quarantine
drwxr-xr-x@   5 root  wheel   160 May  4 01:21 System
drwxr-xr-x    6 root  admin   192 Jun 22 11:32 Users
drwxr-xr-x@   4 root  wheel   128 Oct 17 10:48 Volumes
drwxr-xr-x@  37 root  wheel  1184 Oct 13 05:40 bin
drwxrwxr-t    2 root  admin    64 Jun 22 11:32 cores
dr-xr-xr-x    3 root  wheel  4349 Oct 17 10:44 dev
lrwxr-xr-x@   1 root  wheel    11 Jun 22 11:31 etc -> private/etc
dr-xr-xr-x    2 root  wheel     1 Oct 17 11:32 home
-rw-r--r--    1 root  wheel   313 Feb 24  2019 installer.failurerequests
dr-xr-xr-x    2 root  wheel     1 Oct 17 11:32 net
drwxr-xr-x@   3 root  wheel    96 Dec  9  2016 opt
drwxr-xr-x    7 root  wheel   224 Aug 21 12:47 private
drwxr-xr-x@  64 root  wheel  2048 Oct 13 05:40 sbin
lrwxr-xr-x@   1 root  wheel    11 Jun 22 11:31 tmp -> private/tmp
drwxr-xr-x@   9 root  wheel   288 May  4 01:14 usr
lrwxr-xr-x@   1 root  wheel    11 Jun 22 11:31 var -> private/var
                        

6. traceroute Example

In this example, we will execute the traceroute command to show all the IP addresses of all the network nodes between a source and a destination.

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

public class ExecuteShellCommandRuntimeExec {
	public static void main (String [] args) {
		try {
			Process process = Runtime.getRuntime().exec("traceroute google.com"); 
			
			StringBuilder output = new StringBuilder(); 
			
			BufferedReader reader = new BufferedReader(new InputStreamReader (process.getInputStream()));
			
			String line;
			
			while((line = reader.readLine()) != null) {
				output.append(line + "\n");
			}
			
			int exitVal = process.waitFor();
			if (exitVal == 0) {
				System.out.println("Success");
				System.out.println(output);
				System.exit(0);
			} else {
				System.out.println("Something abnormal has haapened :( ");
			}
				
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
                        

Output:

vpn-133-230-1-200:~ softorks$ traceroute softorks.com
traceroute to softorks.com (162.241.252.212), 64 hops max, 52 byte packets
 1  www.routerlogin.com (192.168.1.1)  1.828 ms  1.171 ms  1.076 ms
 2  96.120.5.17 (96.120.5.17)  10.819 ms  9.734 ms  10.236 ms
 3  68.86.108.157 (68.86.108.157)  9.466 ms  10.106 ms  11.188 ms
 4  ae-127-ar01.b0atlanta.ga.atlanta.comcast.net (68.85.111.57)  10.193 ms  17.725 ms  10.128 ms
 5  be-7725-cr02.56marietta.ga.ibone.comcast.net (68.86.93.125)  13.317 ms  12.365 ms *
 6  be-11423-cr01.houston.tx.ibone.comcast.net (68.86.85.22)  30.680 ms  25.503 ms  25.118 ms
 7  be-12393-pe01.westwaypark.tx.ibone.comcast.net (68.86.82.130)  25.743 ms  24.762 ms  25.539 ms
 8  as8075-1.2001sixthave.wa.ibone.comcast.net (75.149.230.54)  25.087 ms  25.201 ms  25.575 ms
 9  216.117.50.150 (216.117.50.150)  25.742 ms  26.621 ms  25.381 ms
10  po101.router2b.hou1.net.unifiedlayer.com (162.241.0.9)  25.678 ms  25.893 ms
    po101.router2a.hou1.net.unifiedlayer.com (162.241.0.7)  25.370 ms
11  162-241-150-61.unifiedlayer.com (162.241.150.61)  26.501 ms
    162-241-150-59.unifiedlayer.com (162.241.150.59)  25.636 ms *
12  box5741.bluehost.com (162.241.252.212)  28.384 ms  26.011 ms  24.808 ms
                        

7. Watch Video Tutorial...