1515import java .nio .file .Paths ;
1616import java .util .ArrayList ;
1717import java .util .List ;
18- import java .util .ListIterator ;
1918import java .util .StringTokenizer ;
2019
2120/** Fetches and captures the JVM options. */
@@ -41,28 +40,35 @@ private String[] readProcFsCmdLine() {
4140 return null ;
4241 }
4342
44- @ SuppressForbidden // Class.forName() as backup
4543 private List <String > findVmOptions () {
44+ return findVmOptions (PROCFS_CMDLINE );
45+ }
46+
47+ @ SuppressForbidden // Class.forName() as backup
48+ // Visible for testing
49+ List <String > findVmOptions (String [] procfsCmdline ) {
4650 // Try ProcFS on Linux
47- if (PROCFS_CMDLINE != null ) {
51+ // Be aware that when running a native image, the command line in /proc/self/cmdline is just the
52+ // executable
53+ if (procfsCmdline != null ) {
54+ // Create list of VM options
55+ List <String > vmOptions = new ArrayList <>();
4856 // Start at 1 to skip "java" command itself
4957 int index = 1 ;
5058 // Look for main class or "-jar", end of VM options
51- for (; index < PROCFS_CMDLINE .length ; index ++) {
52- if (!PROCFS_CMDLINE [index ].startsWith ("-" ) || "-jar" .equals (PROCFS_CMDLINE [index ])) {
53- break ;
59+ // Simultaneously, collect all arguments in the VM options
60+ for (; index < procfsCmdline .length ; index ++) {
61+ String argument = procfsCmdline [index ];
62+ if (argument .startsWith ("@" )) {
63+ vmOptions .addAll (getArgumentsFromFile (argument ));
64+ } else {
65+ vmOptions .add (argument );
5466 }
55- }
56- // Create list of VM options
57- List <String > vmOptions = new ArrayList <>(asList (PROCFS_CMDLINE ).subList (1 , index + 1 ));
58- ListIterator <String > iterator = vmOptions .listIterator ();
59- while (iterator .hasNext ()) {
60- String vmOption = iterator .next ();
61- if (vmOption .startsWith ("@" )) {
62- iterator .remove ();
63- for (String argument : getArgumentsFromFile (vmOption )) {
64- iterator .add (argument );
67+ if (!argument .startsWith ("-" ) || "-jar" .equals (argument )) {
68+ if (index + 1 < procfsCmdline .length ) {
69+ vmOptions .add (procfsCmdline [index + 1 ]); // jar file or the main class
6570 }
71+ break ;
6672 }
6773 }
6874 // Insert JDK_JAVA_OPTIONS at the start if present and supported
0 commit comments