85 |
85 |
86 // To be consistent with the behavior on other platforms, if -jar, -m, or --module |
86 // To be consistent with the behavior on other platforms, if -jar, -m, or --module |
87 // options are used then just return the value (the path to the jar file or module |
87 // options are used then just return the value (the path to the jar file or module |
88 // name with a main class). Otherwise, the main class name is the first part that |
88 // name with a main class). Otherwise, the main class name is the first part that |
89 // is not a Java option (doesn't start with '-' and is not a classpath or a module |
89 // is not a Java option (doesn't start with '-' and is not a classpath or a module |
90 // path). |
90 // whitespace option). |
91 |
91 |
92 for (int i = 1; i < parts.length && mainClass == null; i++) { |
92 for (int i = 1; i < parts.length && mainClass == null; i++) { |
93 if (i < parts.length - 1) { |
93 if (i < parts.length - 1) { |
94 if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) { |
94 if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) { |
95 return parts[i + 1]; |
95 return parts[i + 1]; |
96 } |
96 } |
97 } |
97 } |
98 // If this is a classpath or a module path option then skip the next part |
98 |
99 // (the classpath or the module path itself) |
99 if (parts[i].startsWith("--module=")) { |
|
100 return parts[i].substring("--module=".length()); |
|
101 } |
|
102 |
|
103 // If this is a classpath or a module whitespace option then skip the next part |
|
104 // (the classpath or the option value itself) |
100 if (parts[i].equals("-cp") || parts[i].equals("-classpath") || parts[i].equals("--class-path") || |
105 if (parts[i].equals("-cp") || parts[i].equals("-classpath") || parts[i].equals("--class-path") || |
101 parts[i].equals("-p") || parts[i].equals("--module-path")) { |
106 isModuleWhiteSpaceOption(parts[i])) { |
102 i++; |
107 i++; |
103 continue; |
108 continue; |
104 } |
109 } |
105 // Skip all other Java options |
110 // Skip all other Java options |
106 if (parts[i].startsWith("-")) { |
111 if (parts[i].startsWith("-")) { |
107 continue; |
112 continue; |
108 } |
113 } |
|
114 |
|
115 // If it is a source-file mode then return null |
|
116 if (parts[i].endsWith(".java")) { |
|
117 return null; |
|
118 } |
|
119 |
109 mainClass = parts[i]; |
120 mainClass = parts[i]; |
110 } |
121 } |
111 return mainClass; |
122 return mainClass; |
112 |
123 |
113 } |
124 } |
114 |
125 |
115 private static String getCommandLine(String pid) { |
126 private static String getCommandLine(String pid) { |
116 try (Stream<String> lines = |
127 try (Stream<String> lines = |
117 Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) { |
128 Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) { |
118 return lines.map(x -> x.replaceAll("\0", " ")).findFirst().orElse(null); |
129 return lines.findFirst().orElse(null); |
119 } catch (IOException | UncheckedIOException e) { |
130 } catch (IOException | UncheckedIOException e) { |
120 return null; |
131 return null; |
121 } |
132 } |
122 } |
133 } |
|
134 |
|
135 private static boolean isModuleWhiteSpaceOption(String option) { |
|
136 return option.equals("-p") || |
|
137 option.equals("--module-path") || |
|
138 option.equals("--upgrade-module-path") || |
|
139 option.equals("--add-modules") || |
|
140 option.equals("--limit-modules") || |
|
141 option.equals("--add-exports") || |
|
142 option.equals("--add-opens") || |
|
143 option.equals("--add-reads") || |
|
144 option.equals("--patch-module"); |
|
145 } |
|
146 |
123 } |
147 } |
124 |
148 |
125 |
149 |