
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to create a process using ProcessBuilder in Java 9?
Java 9 added ProcessHandle interface to Process API to enhance Process class. An instance of the ProcessHandle interface identifies a local process that allows us to query process status and managing processes, and ProcessHandle.Info allows us to use local code because of the need to obtain the PID of a local process.
ProcessBuilder class can be used to create separate operating system processes. In the below example, we can create a process of "notepad" application by using the ProcessBuilder class.
Example
import java.time.ZoneId; import java.util.stream.Stream; import java.util.stream.Collectors; import java.io.IOException; public class ProcessBuilderTest { public static void main(String args[]) throws IOException { ProcessBuilder pb = new ProcessBuilder("notepad.exe"); String np = "Not Present"; Process p = pb.start(); ProcessHandle.Info info = p.info(); System.out.printf("Process ID : %s%n", p.pid()); System.out.printf("Command name : %s%n", info.command().orElse(np)); System.out.printf("Command line : %s%n", info.commandLine().orElse(np)); System.out.printf("Start time: %s%n", info.startInstant().map(i -> i.atZone(ZoneId.systemDefault()).toLocalDateTime().toString()).orElse(np)); System.out.printf("Arguments : %s%n", info.arguments().map(a -> Stream.of(a).collect( Collectors.joining(" "))).orElse(np)); System.out.printf("User : %s%n", info.user().orElse(np)); } }
The above example launches the notepad application as below
Output
Process ID : 3728 Command name : C:\WINDOWS\System32\notepad.exe Command line : Not Present Start time: 2020-04-20T18:06:30.378 Arguments : Not Present User : Tutorialspoint\User
- Related Articles
- How to terminate/destroy a process using Process API in Java 9?
- How to get all children of a process using Process API in Java 9?
- How to traverse a process tree of Process API in Java 9?
- How to create a module in Java 9?
- How to create a thread in JShell in Java 9?
- How to get the parent process of the Process API in Java 9?
- How to create static VarHandle in Java 9?
- How to get a snapshot of information about Process API in Java 9?
- How to create JShell instance programmatically in Java 9?
- How to create Html5 compliant Javadoc in Java 9?
- How to create a class and object in JShell in Java 9?
- How to create a process in Linux?
- How to create scratch variables in JShell in Java 9?
- How to create wrapper objects in JShell in Java 9?
- How can we create a multi-release jar(mrjar) using jar tool in Java 9?

Advertisements