📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:27.033000             🧑  作者: Mango

Java Java类

此类用于创建操作系统进程。每个 ProcessBuilder 实例管理一个流程属性的集合。 start() 方法使用这些属性创建一个新的 Process 实例。可以从同一个实例重复调用 start() 方法来创建具有相同或相关属性的新子流程。

  • ProcessBuilder 可用于帮助创建操作系统进程。
  • 在 JDK 5.0 之前,创建进程并执行它的唯一方法是使用 Runtime.exec() 方法。
  • 它扩展了 Object 类。
  • 此类不同步。

构造函数:

  • ProcessBuilder(List command):这使用指定的操作系统程序和参数构造一个进程构建器。
  • ProcessBuilder(String... command):这会使用指定的操作系统程序和参数构造一个进程构建器。

方法:

1. List command():该方法返回进程构建器的操作系统程序和参数。

Syntax: public List command().
Returns: this process builder's program and its arguments.
Exceptions: NullPointerException - if the argument is null.
Java
// Java code illustrating command() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of process
        List list = new ArrayList();
        list.add("notepad.exe");
 
        // create the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // checking the command i list
        System.out.println("command: " + build.command());
    }
}


Java
// Java code illustrating ProcessBuilder
// command(List command)
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of process
        List list = new ArrayList();
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // create the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // checking the command in list
        System.out.println("command: " + build.command());
    }
}


Java
// Java code illustrating directory() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of process
        List list = new ArrayList();
        list.add("notepad.exe");
        list.add("abc.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // setting the directory
        build.directory(new File("src"));
 
        // checking the directory, on which currently
        // working on
        System.out.println("directory: "
                           + build.directory());
    }
}


Java
// Java code illustrating environment() method
import java.io.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating the process
        ProcessBuilder pb = new ProcessBuilder();
 
        // map view of this process builder's environment
        Map envMap = pb.environment();
 
        // checking map view of environment
        for (Map.Entry entry :
             envMap.entrySet()) {
            // checking key and value separately
            System.out.println("Key = " + entry.getKey()
                               + ", Value = "
                               + entry.getValue());
        }
    }
}


Java
// Java code illustrating redirectErrorStream() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
 
        // creating list of commands
        List list = new ArrayList();
 
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // checking if error stream is redirected
        System.out.println(build.redirectErrorStream());
    }
}


Java
// Java code illustrating redirectErrorStream(boolean
// redirectErrorStream) method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
 
        // creating list of commands
        List list = new ArrayList();
 
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // redirecting error stream
        build.redirectErrorStream(true);
 
        // checking if error stream is redirected
        System.out.println(build.redirectErrorStream());
    }
}


Java
// Java code illustrating start() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of commands
        List commands = new ArrayList();
        commands.add("ls"); // command
        commands.add("-l"); // command
        commands.add(
            "/Users/abhishekverma"); // command in Mac OS
 
        // creating the process
        ProcessBuilder pb = new ProcessBuilder(commands);
 
        // starting the process
        Process process = pb.start();
 
        // for reading the output from stream
        BufferedReader stdInput
            = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
    }
}


Java
// Java code illustrating inheritIO() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg)
        throws IOException, InterruptedException
    {
        ProcessBuilder pb = new ProcessBuilder(
            "echo", "Hello GeeksforGeeks\n"
                        + "This is ProcessBuilder Example");
        pb.inheritIO();
        Process process = pb.start();
        process.waitFor();
    }
}


输出
command: [notepad.exe]

2. ProcessBuilder命令(List command):该方法设置进程构建器的操作系统程序和参数。

Syntax: public ProcessBuilder command(List command).
Returns: NA.
Exception: NullPointerException - if the argument is null.

Java

// Java code illustrating ProcessBuilder
// command(List command)
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of process
        List list = new ArrayList();
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // create the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // checking the command in list
        System.out.println("command: " + build.command());
    }
}
输出
command: [notepad.exe, xyz.txt]

3. ProcessBuilder目录(文件目录):该方法设置进程构建器的工作目录。随后由对象的 start() 方法启动的子进程将使用它作为它们的工作目录。参数可以为null——表示使用当前Java进程的工作目录,通常是系统属性user.dir命名的目录,作为子进程的工作目录。

Syntax: public ProcessBuilder directory(File directory).
Returns: this process builder.
Exception: NA.

Java

// Java code illustrating directory() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of process
        List list = new ArrayList();
        list.add("notepad.exe");
        list.add("abc.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // setting the directory
        build.directory(new File("src"));
 
        // checking the directory, on which currently
        // working on
        System.out.println("directory: "
                           + build.directory());
    }
}
输出
directory: src

4. Map environment():该方法返回流程构建器环境的字符串映射视图。每当创建流程构建器时,都会将环境初始化为当前流程环境的副本。随后由对象的 start() 方法启动的子进程将使用此映射作为它们的环境。

Syntax: public Map environment()
Returns: this process builder's environment
Exception: SecurityException - if a security manager exists 
and its checkPermission method doesn't allow access to the process environment.

Java

// Java code illustrating environment() method
import java.io.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating the process
        ProcessBuilder pb = new ProcessBuilder();
 
        // map view of this process builder's environment
        Map envMap = pb.environment();
 
        // checking map view of environment
        for (Map.Entry entry :
             envMap.entrySet()) {
            // checking key and value separately
            System.out.println("Key = " + entry.getKey()
                               + ", Value = "
                               + entry.getValue());
        }
    }
}

输出:

Key = PATH, Value = /usr/bin:/bin:/usr/sbin:/sbin
Key = JAVA_MAIN_CLASS_14267, Value = ProcessBuilderDemo
Key = J2D_PIXMAPS, Value = shared
Key = SHELL, Value = /bin/bash
Key = JAVA_MAIN_CLASS_11858, Value = org.netbeans.Main
Key = USER, Value = abhishekverma
Key = TMPDIR, Value = /var/folders/9z/p63ysmfd797clc0468vvy4980000gn/T/
Key = SSH_AUTH_SOCK, Value = /private/tmp/com.apple.launchd.uWvCfYQWBP/Listeners
Key = XPC_FLAGS, Value = 0x0
Key = LD_LIBRARY_PATH, Value = /Library/Java/JavaVirtualMachines
/jdk1.8.0_121.jdk/Contents/Home/jre/lib/
amd64:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk
/Contents/Home/jre/lib/i386:
Key = __CF_USER_TEXT_ENCODING, Value = 0x1F5:0x0:0x0
Key = Apple_PubSub_Socket_Render, Value = /private/tmp/com.apple.launchd.weuNq4pAfF/Render
Key = LOGNAME, Value = abhishekverma
Key = LC_CTYPE, Value = UTF-8
Key = XPC_SERVICE_NAME, Value = 0
Key = PWD, Value = /
Key = SHLVL, Value = 1
Key = HOME, Value = /Users/abhishekverma
Key = _, Value = /Library/Java/JavaVirtualMachines/
jdk1.8.0_121.jdk/Contents/Home/bin/java

理解上面的输出:输出是子流程构建的地图视图。上述输出因用户而异,完全取决于操作系统和用户。

5. boolean redirectErrorStream():这个方法告诉流程构建器是否合并标准错误和标准输出。如果此属性为真,则随后由对象的 start() 方法启动的子进程生成的任何错误输出都将与标准输出合并,以便可以使用 Process.getInputStream() 方法读取两者。它使将错误消息与相应的输出关联起来变得更加容易。初始值为假。

Syntax: public boolean redirectErrorStream().
Returns: this process builder's redirectErrorStream property.
Exception: NA.

Java

// Java code illustrating redirectErrorStream() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
 
        // creating list of commands
        List list = new ArrayList();
 
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // checking if error stream is redirected
        System.out.println(build.redirectErrorStream());
    }
}

输出:

false

6. ProcessBuilder redirectErrorStream(boolean redirectErrorStream):此方法设置此流程构建器的redirectErrorStream 属性。如果此属性为真,则随后由该对象的 start() 方法启动的子进程生成的任何错误输出都将与标准输出合并,以便可以使用 Process.getInputStream() 方法读取两者。这使得更容易将错误消息与相应的输出相关联。初始值为假。

Syntax: public ProcessBuilder redirectErrorStream(boolean redirectErrorStream).
Returns: this process builder.
Exception: NA.

Java

// Java code illustrating redirectErrorStream(boolean
// redirectErrorStream) method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
 
        // creating list of commands
        List list = new ArrayList();
 
        list.add("notepad.exe");
        list.add("xyz.txt");
 
        // creating the process
        ProcessBuilder build = new ProcessBuilder(list);
 
        // redirecting error stream
        build.redirectErrorStream(true);
 
        // checking if error stream is redirected
        System.out.println(build.redirectErrorStream());
    }
}

输出:

true

Process start():此方法使用进程构建器的属性启动一个新进程。新进程将在 directory() 给出的工作目录中调用 command() 给出的命令和参数,并使用 environment() 给出的进程环境。此方法检查该命令是否为有效的操作系统命令。哪些命令有效取决于系统,但至少该命令必须是非空字符串的非空列表。如果有安全管理器,则调用它的 checkExec 方法,并使用该对象的命令数组的第一个组件作为其参数。它可能会导致抛出 SecurityException。

Syntax: public Process start().
Returns: a new Process object for managing the subprocess.
Exception: NullPointerException - If an element of the command list is null
IndexOutOfBoundsException - If the command is an empty list (has size 0).
SecurityException - If a security manager exists 
and its checkExec method doesn't allow creation of the subprocess.
IOException - If an I/O error occurs.

Java

// Java code illustrating start() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg) throws IOException
    {
        // creating list of commands
        List commands = new ArrayList();
        commands.add("ls"); // command
        commands.add("-l"); // command
        commands.add(
            "/Users/abhishekverma"); // command in Mac OS
 
        // creating the process
        ProcessBuilder pb = new ProcessBuilder(commands);
 
        // starting the process
        Process process = pb.start();
 
        // for reading the output from stream
        BufferedReader stdInput
            = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
    }
}

输出:

total 0
drwxr-xr-x  10 abhishekverma  staff   340 Jun 20 02:24 AndroidStudioProjects
drwx------@ 22 abhishekverma  staff   748 Jun 20 03:00 Desktop
drwx------@  7 abhishekverma  staff   238 Apr 29 22:03 Documents
drwx------+ 27 abhishekverma  staff   918 Jun 20 03:01 Downloads
drwx------@ 65 abhishekverma  staff  2210 Jun 18 20:48 Library
drwx------+  3 abhishekverma  staff   102 Mar 28 13:08 Movies
drwx------+  4 abhishekverma  staff   136 Apr  8 04:51 Music
drwxr-xr-x   4 abhishekverma  staff   136 Jun 19 15:01 NetBeansProjects
drwx------+  5 abhishekverma  staff   170 Apr 10 09:46 Pictures
drwxr-xr-x+  6 abhishekverma  staff   204 Jun 18 20:45 Public
-rw-r--r--   1 abhishekverma  staff     0 Apr 15 19:23 newreactjs.jsx

ProcessBuilder inheritIO():将子进程标准I/O的源和目标设置为与当前Java进程相同。

Syntax: public ProcessBuilder inheritIO().
Returns: this process builder.
Exception: NA.

Java

// Java code illustrating inheritIO() method
import java.io.*;
import java.lang.*;
import java.util.*;
class ProcessBuilderDemo {
    public static void main(String[] arg)
        throws IOException, InterruptedException
    {
        ProcessBuilder pb = new ProcessBuilder(
            "echo", "Hello GeeksforGeeks\n"
                        + "This is ProcessBuilder Example");
        pb.inheritIO();
        Process process = pb.start();
        process.waitFor();
    }
}

输出:

Hello GeeksforGeeks
This is ProcessBuilder Example