|
24 | 24 | import java.io.IOException;
|
25 | 25 | import java.time.Duration;
|
26 | 26 | import java.util.List;
|
27 |
| -import java.util.NoSuchElementException; |
28 | 27 | import java.util.Objects;
|
29 | 28 | import java.util.Optional;
|
30 | 29 | import java.util.concurrent.ExecutorService;
|
@@ -100,40 +99,36 @@ public void test() throws IOException, InterruptedException {
|
100 | 99 |
|
101 | 100 | private static Optional<Long> findMainAppLauncherPID(JPackageCommand cmd,
|
102 | 101 | int expectedCount) {
|
103 |
| - // Get the list of PIDs and PPIDs of app launcher processes. |
104 |
| - // wmic process where (name = "foo.exe") get ProcessID,ParentProcessID |
105 |
| - List<String> output = Executor.of("wmic", "process", "where", "(name", |
106 |
| - "=", |
107 |
| - "\"" + cmd.appLauncherPath().getFileName().toString() + "\"", |
108 |
| - ")", "get", "ProcessID,ParentProcessID").dumpOutput(true). |
109 |
| - saveOutput().executeAndGetOutput(); |
110 |
| - |
111 |
| - if (output.isEmpty()) { |
112 |
| - throw new NoSuchElementException(); |
113 |
| - } |
114 |
| - String first = output.get(0); |
| 102 | + // Get the list of PIDs and PPIDs of app launcher processes. Run setWinRunWithEnglishOutput(true) for JDK-8344275. |
| 103 | + // powershell -NoLogo -NoProfile -NonInteractive -Command |
| 104 | + // "Get-CimInstance Win32_Process -Filter \"Name = 'foo.exe'\" | select ProcessID,ParentProcessID" |
| 105 | + String command = "Get-CimInstance Win32_Process -Filter \\\"Name = '" |
| 106 | + + cmd.appLauncherPath().getFileName().toString() |
| 107 | + + "'\\\" | select ProcessID,ParentProcessID"; |
| 108 | + List<String> output = Executor.of("powershell", "-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command) |
| 109 | + .dumpOutput(true).saveOutput().executeAndGetOutput(); |
115 | 110 |
|
116 | 111 | if (expectedCount == 0) {
|
117 |
| - TKit.assertEquals("No Instance(s) Available.", first. |
118 |
| - trim(), "Check no app launcher processes found running"); |
119 |
| - return Optional.empty(); |
| 112 | + if (output.size() < 1) { |
| 113 | + return Optional.empty(); |
| 114 | + } |
120 | 115 | }
|
121 | 116 |
|
122 |
| - String[] headers = Stream.of(first.split("\\s+", 2)).map( |
| 117 | + String[] headers = Stream.of(output.get(1).split("\\s+", 2)).map( |
123 | 118 | String::trim).map(String::toLowerCase).toArray(String[]::new);
|
124 | 119 | Pattern pattern;
|
125 | 120 | if (headers[0].equals("parentprocessid") && headers[1].equals(
|
126 | 121 | "processid")) {
|
127 |
| - pattern = Pattern.compile("^(?<ppid>\\d+)\\s+(?<pid>\\d+)\\s+$"); |
| 122 | + pattern = Pattern.compile("^\\s+(?<ppid>\\d+)\\s+(?<pid>\\d+)$"); |
128 | 123 | } else if (headers[1].equals("parentprocessid") && headers[0].equals(
|
129 | 124 | "processid")) {
|
130 |
| - pattern = Pattern.compile("^(?<pid>\\d+)\\s+(?<ppid>\\d+)\\s+$"); |
| 125 | + pattern = Pattern.compile("^\\s+(?<pid>\\d+)\\s+(?<ppid>\\d+)$"); |
131 | 126 | } else {
|
132 | 127 | throw new RuntimeException(
|
133 |
| - "Unrecognizable output of \'wmic process\' command"); |
| 128 | + "Unrecognizable output of \'Get-CimInstance Win32_Process\' command"); |
134 | 129 | }
|
135 | 130 |
|
136 |
| - List<long[]> processes = output.stream().skip(1).map(line -> { |
| 131 | + List<long[]> processes = output.stream().skip(3).map(line -> { |
137 | 132 | Matcher m = pattern.matcher(line);
|
138 | 133 | long[] pids = null;
|
139 | 134 | if (m.matches()) {
|
|
0 commit comments