8213362: [macOS] Could not find libjava.dylib error when initializing JVM via JNI_CreateJavaVM
authorhenryjen
Fri, 30 Nov 2018 13:42:49 -0800
changeset 52788 241b8151b6b6
parent 52787 cb009cf888c6
child 52789 a051c5c8aa56
8213362: [macOS] Could not find libjava.dylib error when initializing JVM via JNI_CreateJavaVM Reviewed-by: alanb, ihse Contributed-by: priyanka.mangal@oracle.com
make/test/JtregNativeJdk.gmk
src/java.base/macosx/native/libjli/java_md_macosx.m
test/jdk/tools/launcher/JliLaunchTest.java
test/jdk/tools/launcher/exeJliLaunchTest.c
test/jdk/vm/JniInvocationTest.java
test/jdk/vm/exeJniInvocationTest.c
--- a/make/test/JtregNativeJdk.gmk	Fri Nov 30 12:41:00 2018 -0800
+++ b/make/test/JtregNativeJdk.gmk	Fri Nov 30 13:42:49 2018 -0800
@@ -48,12 +48,19 @@
 
 BUILD_JDK_JTREG_IMAGE_DIR := $(TEST_IMAGE_DIR)/jdk/jtreg
 
+BUILD_JDK_JTREG_EXECUTABLES_CFLAGS_exeJliLaunchTest := \
+    -I$(TOPDIR)/src/java.base/share/native/libjli \
+    -I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS_TYPE)/native/libjli \
+    -I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS)/native/libjli
+
 # Platform specific setup
 ifeq ($(OPENJDK_TARGET_OS), windows)
   BUILD_JDK_JTREG_EXCLUDE += libDirectIO.c libInheritedChannel.c
 
   WIN_LIB_JAVA := $(SUPPORT_OUTPUTDIR)/native/java.base/libjava/java.lib
   BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := $(WIN_LIB_JAVA)
+  WIN_LIB_JLI := $(SUPPORT_OUTPUTDIR)/native/java.base/libjli/jli.lib
+  BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := $(WIN_LIB_JLI)
 else
   BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
   BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
@@ -62,14 +69,17 @@
   else ifeq ($(OPENJDK_TARGET_OS), solaris)
     BUILD_JDK_JTREG_LIBRARIES_LIBS_libInheritedChannel := -ljava -lsocket -lnsl
   endif
+  BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := -ljli
 endif
 
 ifeq ($(OPENJDK_TARGET_OS), macosx)
   BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libTestMainKeyWindow := -ObjC
   BUILD_JDK_JTREG_LIBRARIES_LIBS_libTestMainKeyWindow := -framework JavaVM \
       -framework Cocoa -framework JavaNativeFoundation
+  BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJniInvocationTest := -ljli
 else
   BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.c
+  BUILD_JDK_JTREG_EXCLUDE += exeJniInvocationTest.c
 endif
 
 $(eval $(call SetupTestFilesCompilation, BUILD_JDK_JTREG_LIBRARIES, \
--- a/src/java.base/macosx/native/libjli/java_md_macosx.m	Fri Nov 30 12:41:00 2018 -0800
+++ b/src/java.base/macosx/native/libjli/java_md_macosx.m	Fri Nov 30 13:42:49 2018 -0800
@@ -508,7 +508,7 @@
         return JNI_FALSE;
     }
 
-    const char lastPathComponent[] = "/lib/jli/libjli.dylib";
+    const char lastPathComponent[] = "/lib/libjli.dylib";
     size_t sizeOfLastPathComponent = sizeof(lastPathComponent) - 1;
     if (pathLen < sizeOfLastPathComponent) {
         return JNI_FALSE;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/launcher/JliLaunchTest.java	Fri Nov 30 13:42:49 2018 -0800
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+/**
+ * @test
+ * @bug 8213362
+ * @comment Test JLI_Launch for tools distributed outside JDK
+ * @library /test/lib
+ * @run main/native JliLaunchTest
+ */
+
+import java.util.Map;
+import jdk.test.lib.Utils;
+import jdk.test.lib.Platform;
+import jdk.test.lib.process.OutputAnalyzer;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class JliLaunchTest {
+    public static void main(String[] args) throws IOException {
+        Path launcher = Paths.get(System.getProperty("test.nativepath"),
+            "JliLaunchTest" + (Platform.isWindows() ? ".exe" : ""));
+        System.out.println("Launcher = " + launcher + (Files.exists(launcher) ? " (exists)" : " (not exists)"));
+        ProcessBuilder pb = new ProcessBuilder(launcher.toString(), "--version");
+        Map<String, String> env = pb.environment();
+        if (Platform.isWindows()) {
+            // The DLL should be in JDK/bin
+        } else {
+            String libdir = Paths.get(Utils.TEST_JDK).resolve("lib").toAbsolutePath().toString();
+            String LD_LIBRARY_PATH = Platform.isOSX() ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH";
+            env.compute(LD_LIBRARY_PATH, (k, v) -> (k == null) ? libdir : v + ":" + libdir);
+        }
+
+        OutputAnalyzer outputf = new OutputAnalyzer(pb.start());
+        outputf.shouldHaveExitValue(0);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/tools/launcher/exeJliLaunchTest.c	Fri Nov 30 13:42:49 2018 -0800
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+
+/*
+ * This file contains the main entry point into the launcher code
+ * this is the only file which will be repeatedly compiled by other
+ * tools. The rest of the files will be linked in.
+ */
+
+#include "java.h"
+
+int
+main(int argc, char **argv)
+{
+    return JLI_Launch(argc, argv,
+                   0, NULL,
+                   0, NULL,
+                   "1", "0",
+                   *argv, *argv,
+                   0, 0, 0, 0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/vm/JniInvocationTest.java	Fri Nov 30 13:42:49 2018 -0800
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+/**
+ * @test
+ * @bug 8213362
+ * @comment Test uses custom launcher that starts VM using JNI via libjli, only for MacOS
+ * @requires os.family == "mac"
+ * @library /test/lib
+ * @run main/native JniInvocationTest
+ */
+
+import java.util.Map;
+import jdk.test.lib.Utils;
+import jdk.test.lib.process.OutputAnalyzer;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class JniInvocationTest {
+    public static void main(String[] args) throws IOException {
+        Path launcher = Paths.get(System.getProperty("test.nativepath"), "JniInvocationTest");
+        System.out.println("Launcher = " + launcher + (Files.exists(launcher) ? " (exists)" : " (not exists)"));
+        ProcessBuilder pb = new ProcessBuilder(launcher.toString());
+        Map<String, String> env = pb.environment();
+        String libdir = Paths.get(Utils.TEST_JDK).resolve("lib").toAbsolutePath().toString();
+        env.compute("DYLD_LIBRARY_PATH", (k, v) -> (k == null) ? libdir : v + ":" + libdir);
+        OutputAnalyzer outputf = new OutputAnalyzer(pb.start());
+        outputf.shouldHaveExitValue(0);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/vm/exeJniInvocationTest.c	Fri Nov 30 13:42:49 2018 -0800
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ *
+ */
+
+#include "jni.h"
+#include "stdio.h"
+#include "stdlib.h"
+
+int main(int argc, char** args) {
+    JavaVMInitArgs vm_args;
+    JNIEnv *env;
+    JavaVM *vm;
+    int i =0;
+    jint result;
+
+    vm_args.version = JNI_VERSION_1_2;
+    vm_args.ignoreUnrecognized = JNI_FALSE;
+
+    JavaVMOption option1[2];
+    option1[0].optionString="-XX:+PrintCommandLineFlags";
+    option1[1].optionString="-Xrs";
+
+    vm_args.options=option1;
+    vm_args.nOptions=2;
+
+    // Print the VM options in use
+    printf("initVM: numOptions = %d\n", vm_args.nOptions);
+    for (i = 0; i < vm_args.nOptions; i++)
+    {
+        printf("\tvm_args.options[%d].optionString = %s\n", i, vm_args.options[i].optionString);
+    }
+
+    // Initialize VM with given options
+    result = JNI_CreateJavaVM( &vm, (void **) &env, &vm_args );
+    if (result != 0) {
+        printf("ERROR: cannot create JAVA VM.\n");
+        exit(-1);
+    }
+
+    (*vm)->DestroyJavaVM(vm);
+}
+