|
|
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
|
|
|
|
|
List Of All ProcessBuilder Class Sample Programs:- How to invoke other applicatons in java?
- How to run operating system specific command and read its output?
- How to get process environment variables in java at runtime?
- How to run ProcessBuilder with list of commands?
|
|
|
What is servlet context?
The servlet context is an interface which helps to communicate with
other servlets. It contains information about the Web application and
container. It is kind of application environment. Using the context, a
servlet can obtain URL references to resources, and store attributes that
other servlets in the context can use.
The greatest obstacle to discovery is not ignorance; it is the illusion of knowledge.
-- Daniel J. Boorstin
|