JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

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
[email protected]  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
[email protected]  9 root  846622648       306 Aug 23 10:03 Dropbox
[email protected] 20 root  846622648       680 Aug 23 10:03 Google Drive
drwxrwxr-x   4 root       admin           136 Feb  6  2012 JProbe
[email protected] 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
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
You can never get enough of what you don’t really need.
-- Eric Hoffer

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 © 2020 by Nataraja Gootooru. All Rights Reserved.