11/*
2- * Copyright (c) 1995, 2020 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 1995, 2021 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -284,9 +284,8 @@ JLI_Launch(int argc, char ** argv, /* main argc, argv */
284284 jvmpath , sizeof (jvmpath ),
285285 jvmcfg , sizeof (jvmcfg ));
286286
287- if (!IsJavaArgs ()) {
288- SetJvmEnvironment (argc ,argv );
289- }
287+ /* Set env. Must be done before LoadJavaVM. */
288+ SetJvmEnvironment (argc , argv );
290289
291290 ifn .CreateJavaVM = 0 ;
292291 ifn .GetDefaultJavaVMInitArgs = 0 ;
@@ -806,18 +805,22 @@ static void
806805SetJvmEnvironment (int argc , char * * argv ) {
807806
808807 static const char * NMT_Env_Name = "NMT_LEVEL_" ;
808+ const char * NMT_Arg_Name = IsJavaArgs () ? "-J-XX:NativeMemoryTracking=" : "-XX:NativeMemoryTracking=" ;
809809 int i ;
810810 /* process only the launcher arguments */
811811 for (i = 0 ; i < argc ; i ++ ) {
812812 char * arg = argv [i ];
813813 /*
814- * Since this must be a VM flag we stop processing once we see
815- * an argument the launcher would not have processed beyond (such
816- * as -version or -h), or an argument that indicates the following
817- * arguments are for the application (i.e. the main class name, or
818- * the -jar argument).
814+ * Java launcher (!IsJavaArgs()):
815+ * Since this must be a VM flag we stop processing once we see
816+ * an argument the launcher would not have processed beyond (such
817+ * as -version or -h), or an argument that indicates the following
818+ * arguments are for the application (i.e. the main class name, or
819+ * the -jar argument).
820+ * Other launchers (IsJavaArgs()):
821+ * All arguments have to be scanned to see if it is a -J argument.
819822 */
820- if (i > 0 ) {
823+ if (! IsJavaArgs () && i > 0 ) {
821824 char * prev = argv [i - 1 ];
822825 // skip non-dash arg preceded by class path specifiers
823826 if (* arg != '-' && IsWhiteSpaceOption (prev )) {
@@ -835,10 +838,10 @@ SetJvmEnvironment(int argc, char **argv) {
835838 * The argument is passed to the JVM, which will check validity.
836839 * The JVM is responsible for removing the env variable.
837840 */
838- if (JLI_StrCCmp (arg , "-XX:NativeMemoryTracking=" ) == 0 ) {
841+ if (JLI_StrCCmp (arg , NMT_Arg_Name ) == 0 ) {
839842 int retval ;
840843 // get what follows this parameter, include "="
841- size_t pnlen = JLI_StrLen ("-XX:NativeMemoryTracking=" );
844+ size_t pnlen = JLI_StrLen (NMT_Arg_Name );
842845 if (JLI_StrLen (arg ) > pnlen ) {
843846 char * value = arg + pnlen ;
844847 size_t pbuflen = pnlen + JLI_StrLen (value ) + 10 ; // 10 max pid digits
0 commit comments