diff --git a/make/conf/module-loader-map.conf b/make/conf/module-loader-map.conf index b628bfbf2da..92bffc0e9bc 100644 --- a/make/conf/module-loader-map.conf +++ b/make/conf/module-loader-map.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2014, 2025, 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 @@ -103,6 +103,7 @@ NATIVE_ACCESS_MODULES= \ java.smartcardio \ jdk.accessibility \ jdk.attach \ + jdk.compiler \ jdk.crypto.cryptoki \ jdk.crypto.mscapi \ jdk.hotspot.agent \ diff --git a/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java b/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java index f5904915e26..e8a9281fbea 100644 --- a/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java +++ b/src/java.base/share/classes/jdk/internal/module/ModuleBootstrap.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2025, 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 @@ -834,9 +834,15 @@ public final class ModuleBootstrap { /** * Grants native access to modules selected using the --enable-native-access * command line option, and also to JDK modules that need the access. + *
+ * In case of being in "source" launcher mode, warnings about unknown modules are
+ * deferred to the source launcher logic in the jdk.compiler module, as those
+ * modules might be not compiled, yet.
*/
private static void addEnableNativeAccess(ModuleLayer layer) {
- addEnableNativeAccess(layer, USER_NATIVE_ACCESS_MODULES, true);
+ String launcherMode = getAndRemoveProperty("sun.java.launcher.mode");
+ boolean shouldWarn = !"source".equals(launcherMode);
+ addEnableNativeAccess(layer, USER_NATIVE_ACCESS_MODULES, shouldWarn);
addEnableNativeAccess(layer, JDK_NATIVE_ACCESS_MODULES, false);
}
diff --git a/src/java.base/share/native/libjli/java.c b/src/java.base/share/native/libjli/java.c
index e88d104452d..a36b91e29af 100644
--- a/src/java.base/share/native/libjli/java.c
+++ b/src/java.base/share/native/libjli/java.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2025, 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
@@ -1374,6 +1374,8 @@ ParseArguments(int *pargc, char ***pargv,
}
if (mode == LM_SOURCE) {
+ // communicate the launcher mode to runtime
+ AddOption("-Dsun.java.launcher.mode=source", NULL);
AddOption("--add-modules=ALL-DEFAULT", NULL);
*pwhat = SOURCE_LAUNCHER_MAIN_ENTRY;
// adjust (argc, argv) so that the name of the source file
diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java
index 941f5c4c40e..5fa76c14d3f 100644
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/launcher/MemoryContext.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2023, 2025, 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
@@ -208,7 +208,9 @@ final class MemoryContext {
var modulePathModules = modulePathFinder.findAll().stream().map(ModuleReference::descriptor).map(ModuleDescriptor::name).toList();
if (!modulePathModules.isEmpty()) {
var modulePathConfiguration = bootLayer.configuration().resolveAndBind(modulePathFinder, ModuleFinder.of(), Set.copyOf(modulePathModules));
- var modulePathLayer = ModuleLayer.defineModulesWithOneLoader(modulePathConfiguration, List.of(bootLayer), parent).layer();
+ var modulePathController = ModuleLayer.defineModulesWithOneLoader(modulePathConfiguration, List.of(bootLayer), parent);
+ enableNativeAccess(modulePathController, false);
+ var modulePathLayer = modulePathController.layer();
parentLayer = modulePathLayer;
parentLoader = modulePathLayer.findLoader(modulePathModules.getFirst());
}
@@ -226,6 +228,9 @@ final class MemoryContext {
var mainClassNamePackageName = mainClassName.substring(0, lastDotInMainClassName);
memoryController.addOpens(module, mainClassNamePackageName, getClass().getModule());
+ // Configure native access for the modular application.
+ enableNativeAccess(memoryController, true);
+
return memoryLayer.findLoader(applicationModule.name());
}
@@ -238,6 +243,33 @@ final class MemoryContext {
return ModuleFinder.of(paths.toArray(Path[]::new));
}
+ /**
+ * Grants native access to modules selected using the --enable-native-access
+ * command line option.
+ */
+ @SuppressWarnings("restricted")
+ private void enableNativeAccess(ModuleLayer.Controller controller, boolean shouldWarn) {
+ var layer = controller.layer();
+ for (var name : options.enableNativeAccessForModules()) {
+ if (name.equals("ALL-UNNAMED")) {
+ continue; // was taken care of by module bootstrap
+ }
+ var found = layer.findModule(name);
+ if (found.isEmpty()) {
+ if (shouldWarn) {
+ // same message as ModuleBootstrap.warnUnknownModule(ENABLE_NATIVE_ACCESS, name);
+ out.println("WARNING: Unknown module: " + name + " specified to --enable-native-access");
+ }
+ continue;
+ }
+ var module = found.get();
+ if (module.isNativeAccessEnabled()) {
+ continue;
+ }
+ controller.enableNativeAccess(module);
+ }
+ }
+
static class MemoryPreview extends Preview {
static void registerInstance(Context context) {
context.put(previewKey, (Factory