JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to run ProcessBuilder with list of commands?


Description:

Below example shows how to run ProcessBuilder with a command list. The list contains the actual command and its arguments.


Code:
package com.java2novice.processbuilder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyMultipleCommandsEx {

	public static void main(String a[]){
		
		InputStream is = null;
		ByteArrayOutputStream baos = null;
		List<String> commands = new ArrayList<String>();
		commands.add("ls");
		commands.add("-l");
		commands.add("/Users/java2novice/");
		ProcessBuilder pb = new ProcessBuilder(commands);
		try {
			Process prs = pb.start();
			is = prs.getInputStream();
			byte[] b = new byte[1024];
			int size = 0;
			baos = new ByteArrayOutputStream();
			while((size = is.read(b)) != -1){
				baos.write(b, 0, size);
			}
			System.out.println(new String(baos.toByteArray()));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(is != null) is.close();
				if(baos != null) baos.close();
			} catch (Exception ex){}
		}
	}
}

Output:
total 170592
-rw-r--r--@  1 root  846622648       254 Feb  2  2012 197-89131.license
drwxr-xr-x   7 root  846622648       238 Feb 21  2012 Android UI Patterns
drwxr-xr-x   6 root  846622648       204 May 31 19:05 Android-dev
drwxr-xr-x   6 root  846622648       204 Jul 13  2011 Automation Test
drwx------+ 30 root  846622648      1020 Aug 24 19:04 Desktop
drwx------+ 13 root  846622648       442 Jun 11 21:22 Documents
drwx------+ 78 root  846622648      2652 Aug 26 14:37 Downloads
drwx------@  9 root  846622648       306 Aug 23 10:03 Dropbox
rwxr-xr-x@ 20 root  846622648       680 Aug 23 10:03 Google Drive
drwxrwxr-x   4 root       admin           136 Feb  6  2012 JProbe
drwx------@ 44 root  846622648      1496 Jul 15 19:45 Library
<< Previous Program 

List Of All ProcessBuilder Class Sample Programs:

  1. How to invoke other applicatons in java?
  2. How to run operating system specific command and read its output?
  3. How to get process environment variables in java at runtime?
  4. How to run ProcessBuilder with list of commands?
Knowledge Centre
Java class can be private?
We can not declare top level class as private. Java allows only public and default modifier for top level classes in java. Inner classes can be private.
Famous Quotations
Success consists of going from failure to failure without loss of enthusiasm.
-- Winston Churchill

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.