forked from nsquare-jdzone/java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessApiDemo.java
More file actions
114 lines (101 loc) · 4.7 KB
/
ProcessApiDemo.java
File metadata and controls
114 lines (101 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package com.javadeveloperzone;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by nitin on 4/1/2017.
*/
public class ProcessApiDemo {
private void printProcessId(){
//ProcessHandle.current() method will return current process's ProcessHandle
ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
//java 9 ProcessHandleImpl class has static block, which look like below
/*static {
initNative();
long pid = getCurrentPid0(); //it's native method
current = new ProcessHandleImpl(pid, isAlive0(pid));
}*/
//getid method return current process Native PID
System.out.println("Current Process Native PID : "+currentProcessHandleImpl.getPid());
}
private static final String NULL = "NULL";
private void printProcessSnapShot(){
ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
ProcessHandle.Info processInfo = currentProcessHandleImpl.info();
String [] defaults = new String[]{"-"};
System.out.print("Start arguments : ");
for(String argument : processInfo.arguments().orElse(defaults)){
System.out.print(argument+" - ");
}
System.out.println("Command : "+processInfo.command().orElse(NULL));
System.out.println("CommandLine : "+processInfo.commandLine().orElse(NULL));
System.out.println("User : "+processInfo.user().orElse(NULL));
System.out.println("Start Time : " + processInfo.startInstant().
orElse(Instant.now()).toString());
}
public void printChildrenProcess(){
ProcessHandle currentProcessHandleImpl = ProcessHandle.current();
try {
Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
Process notepadProcessTwo = new ProcessBuilder("notepad.exe").start();
} catch (IOException e) {
e.printStackTrace();
}
List<ProcessHandle> childrenProcess = currentProcessHandleImpl.children().collect(Collectors.toList());
int childrenCount=1;
for(ProcessHandle processHandle : childrenProcess){
System.out.println("Children process "+(childrenCount++) + " "+processHandle.getPid());
}
}
public void printParentProcess(){
try {
Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
ProcessHandle parenthandle = notepadProcessOne.toHandle().parent().get();
System.out.println("Parent Process Native PID : "+parenthandle.getPid());
} catch (IOException e) {
e.printStackTrace();
}
}
public void checkProcessIsAlive(){
try {
Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
ProcessHandle notepadProcessHandle = notepadProcessOne.toHandle();
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
notepadProcessHandle.destroy();
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
} catch (IOException e) {
e.printStackTrace();
}
}
public void killProcess(){
try {
Process notepadProcessOne = new ProcessBuilder("notepad.exe").start();
ProcessHandle notepadProcessHandle = notepadProcessOne.toHandle();
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
boolean isKilled = notepadProcessHandle.destroy();
if(isKilled){
System.out.println("Notepad process killed by destroy() !!!");
}
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive()+System.lineSeparator());
notepadProcessOne = new ProcessBuilder("notepad.exe").start();
notepadProcessHandle = notepadProcessOne.toHandle();
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive());
isKilled = notepadProcessHandle.destroyForcibly();
if(isKilled){
System.out.println("Notepad process killed by destroyForcibly() !!!");
}
System.out.println("Notepad Process is running : "+notepadProcessHandle.isAlive()+System.lineSeparator());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ProcessApiDemo apiDemo = new ProcessApiDemo();
//apiDemo.printProcessSnapShot();
//apiDemo.printChildrenProcess();
//apiDemo.printParentProcess();
//apiDemo.checkProcessIsAlive();
apiDemo.killProcess();
}
}