Skip to content

Commit 7c6cb8c

Browse files
Priyanka Mangalslowhog
authored andcommitted
8213362: [macOS] Could not find libjava.dylib error when initializing JVM via JNI_CreateJavaVM
Reviewed-by: alanb, ihse
1 parent 3995ae1 commit 7c6cb8c

File tree

6 files changed

+234
-1
lines changed

6 files changed

+234
-1
lines changed

make/test/JtregNativeJdk.gmk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,19 @@ BUILD_JDK_JTREG_OUTPUT_DIR := $(OUTPUTDIR)/support/test/jdk/jtreg/native
4848

4949
BUILD_JDK_JTREG_IMAGE_DIR := $(TEST_IMAGE_DIR)/jdk/jtreg
5050

51+
BUILD_JDK_JTREG_EXECUTABLES_CFLAGS_exeJliLaunchTest := \
52+
-I$(TOPDIR)/src/java.base/share/native/libjli \
53+
-I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS_TYPE)/native/libjli \
54+
-I$(TOPDIR)/src/java.base/$(OPENJDK_TARGET_OS)/native/libjli
55+
5156
# Platform specific setup
5257
ifeq ($(OPENJDK_TARGET_OS), windows)
5358
BUILD_JDK_JTREG_EXCLUDE += libDirectIO.c libInheritedChannel.c
5459

5560
WIN_LIB_JAVA := $(SUPPORT_OUTPUTDIR)/native/java.base/libjava/java.lib
5661
BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := $(WIN_LIB_JAVA)
62+
WIN_LIB_JLI := $(SUPPORT_OUTPUTDIR)/native/java.base/libjli/jli.lib
63+
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := $(WIN_LIB_JLI)
5764
else
5865
BUILD_JDK_JTREG_LIBRARIES_LIBS_libstringPlatformChars := -ljava
5966
BUILD_JDK_JTREG_LIBRARIES_LIBS_libDirectIO := -ljava
@@ -62,14 +69,17 @@ else
6269
else ifeq ($(OPENJDK_TARGET_OS), solaris)
6370
BUILD_JDK_JTREG_LIBRARIES_LIBS_libInheritedChannel := -ljava -lsocket -lnsl
6471
endif
72+
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJliLaunchTest := -ljli
6573
endif
6674

6775
ifeq ($(OPENJDK_TARGET_OS), macosx)
6876
BUILD_JDK_JTREG_LIBRARIES_CFLAGS_libTestMainKeyWindow := -ObjC
6977
BUILD_JDK_JTREG_LIBRARIES_LIBS_libTestMainKeyWindow := -framework JavaVM \
7078
-framework Cocoa -framework JavaNativeFoundation
79+
BUILD_JDK_JTREG_EXECUTABLES_LIBS_exeJniInvocationTest := -ljli
7180
else
7281
BUILD_JDK_JTREG_EXCLUDE += libTestMainKeyWindow.c
82+
BUILD_JDK_JTREG_EXCLUDE += exeJniInvocationTest.c
7383
endif
7484

7585
$(eval $(call SetupTestFilesCompilation, BUILD_JDK_JTREG_LIBRARIES, \

src/java.base/macosx/native/libjli/java_md_macosx.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ static void MacOSXStartup(int argc, char *argv[]) {
508508
return JNI_FALSE;
509509
}
510510

511-
const char lastPathComponent[] = "/lib/jli/libjli.dylib";
511+
const char lastPathComponent[] = "/lib/libjli.dylib";
512512
size_t sizeOfLastPathComponent = sizeof(lastPathComponent) - 1;
513513
if (pathLen < sizeOfLastPathComponent) {
514514
return JNI_FALSE;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
/**
26+
* @test
27+
* @bug 8213362
28+
* @comment Test JLI_Launch for tools distributed outside JDK
29+
* @library /test/lib
30+
* @run main/native JliLaunchTest
31+
*/
32+
33+
import java.util.Map;
34+
import jdk.test.lib.Utils;
35+
import jdk.test.lib.Platform;
36+
import jdk.test.lib.process.OutputAnalyzer;
37+
38+
import java.io.IOException;
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.Paths;
42+
43+
public class JliLaunchTest {
44+
public static void main(String[] args) throws IOException {
45+
Path launcher = Paths.get(System.getProperty("test.nativepath"),
46+
"JliLaunchTest" + (Platform.isWindows() ? ".exe" : ""));
47+
System.out.println("Launcher = " + launcher + (Files.exists(launcher) ? " (exists)" : " (not exists)"));
48+
ProcessBuilder pb = new ProcessBuilder(launcher.toString(), "--version");
49+
Map<String, String> env = pb.environment();
50+
if (Platform.isWindows()) {
51+
// The DLL should be in JDK/bin
52+
} else {
53+
String libdir = Paths.get(Utils.TEST_JDK).resolve("lib").toAbsolutePath().toString();
54+
String LD_LIBRARY_PATH = Platform.isOSX() ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH";
55+
env.compute(LD_LIBRARY_PATH, (k, v) -> (k == null) ? libdir : v + ":" + libdir);
56+
}
57+
58+
OutputAnalyzer outputf = new OutputAnalyzer(pb.start());
59+
outputf.shouldHaveExitValue(0);
60+
}
61+
}
62+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
27+
/*
28+
* This file contains the main entry point into the launcher code
29+
* this is the only file which will be repeatedly compiled by other
30+
* tools. The rest of the files will be linked in.
31+
*/
32+
33+
#include "java.h"
34+
35+
int
36+
main(int argc, char **argv)
37+
{
38+
return JLI_Launch(argc, argv,
39+
0, NULL,
40+
0, NULL,
41+
"1", "0",
42+
*argv, *argv,
43+
0, 0, 0, 0);
44+
}

test/jdk/vm/JniInvocationTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
/**
26+
* @test
27+
* @bug 8213362
28+
* @comment Test uses custom launcher that starts VM using JNI via libjli, only for MacOS
29+
* @requires os.family == "mac"
30+
* @library /test/lib
31+
* @run main/native JniInvocationTest
32+
*/
33+
34+
import java.util.Map;
35+
import jdk.test.lib.Utils;
36+
import jdk.test.lib.process.OutputAnalyzer;
37+
38+
import java.io.IOException;
39+
import java.nio.file.Files;
40+
import java.nio.file.Path;
41+
import java.nio.file.Paths;
42+
43+
public class JniInvocationTest {
44+
public static void main(String[] args) throws IOException {
45+
Path launcher = Paths.get(System.getProperty("test.nativepath"), "JniInvocationTest");
46+
System.out.println("Launcher = " + launcher + (Files.exists(launcher) ? " (exists)" : " (not exists)"));
47+
ProcessBuilder pb = new ProcessBuilder(launcher.toString());
48+
Map<String, String> env = pb.environment();
49+
String libdir = Paths.get(Utils.TEST_JDK).resolve("lib").toAbsolutePath().toString();
50+
env.compute("DYLD_LIBRARY_PATH", (k, v) -> (k == null) ? libdir : v + ":" + libdir);
51+
OutputAnalyzer outputf = new OutputAnalyzer(pb.start());
52+
outputf.shouldHaveExitValue(0);
53+
}
54+
}
55+

test/jdk/vm/exeJniInvocationTest.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#include "jni.h"
26+
#include "stdio.h"
27+
#include "stdlib.h"
28+
29+
int main(int argc, char** args) {
30+
JavaVMInitArgs vm_args;
31+
JNIEnv *env;
32+
JavaVM *vm;
33+
int i =0;
34+
jint result;
35+
36+
vm_args.version = JNI_VERSION_1_2;
37+
vm_args.ignoreUnrecognized = JNI_FALSE;
38+
39+
JavaVMOption option1[2];
40+
option1[0].optionString="-XX:+PrintCommandLineFlags";
41+
option1[1].optionString="-Xrs";
42+
43+
vm_args.options=option1;
44+
vm_args.nOptions=2;
45+
46+
// Print the VM options in use
47+
printf("initVM: numOptions = %d\n", vm_args.nOptions);
48+
for (i = 0; i < vm_args.nOptions; i++)
49+
{
50+
printf("\tvm_args.options[%d].optionString = %s\n", i, vm_args.options[i].optionString);
51+
}
52+
53+
// Initialize VM with given options
54+
result = JNI_CreateJavaVM( &vm, (void **) &env, &vm_args );
55+
if (result != 0) {
56+
printf("ERROR: cannot create JAVA VM.\n");
57+
exit(-1);
58+
}
59+
60+
(*vm)->DestroyJavaVM(vm);
61+
}
62+

0 commit comments

Comments
 (0)