Skip to content

Prevent the REPL from warning about restricted java.lang.System API on JDK 24 #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025

Conversation

Gedochao
Copy link
Contributor

@Gedochao Gedochao commented Jun 27, 2025

Fixes scala/scala3#22756

What even happens here?

JLine uses some restricted JDK API, which becomes a warning on JDK 24.
https://docs.oracle.com/en/java/javase/24/core/restricted-methods.html

Why can't we use the approach suggested in the warning?

The API being called can be enabled with --enable-native-access, but it is unfortunately only possible via module name.
Which means the call needs to happen in a named module, on the module path.
The REPL uses a class path, not a module path. The warning suggests ALL-UNNAMED, as everything on the classpath doesn't have a named module and thus is just thrown into the catch-all unnamed bin.
--enable-native-access=ALL-UNNAMED is way too broad, as it could cause users to miss legitimate warnings from their code.

So what do we do instead?

What I do is force JLine on the module path as well as the class path (sigh), let the JVM pick up its default module names from the JAR manifests and then enable the restricted access APIs for those paths only.

Can this break with a future JLine version bump?

Sure it can.
This is fragile, as we can't really rely on this stuff being unchanged in different JLine versions.
You might notice that the named module defaults in manifests of JLine versions used by Scala 2.13 and Scala 3 REPL differ.
I added a test, so that we have a sanity check.
The test is a bit hacky too, but what can you do.
Hacky problems require hacky solutions.

Behaviour examples

Before

  • Scala 3
    scala-cli repl --jvm 24
    # WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
    # WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ (file:/Users/pchabelski/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.7.1/scala3-library_3-3.7.1.jar)
    # WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
    # WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
    # WARNING: A restricted method in java.lang.System has been called
    # WARNING: java.lang.System::load has been called by org.jline.nativ.JLineNativeLoader in an unnamed module (file:/Users/pchabelski/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/jline/jline-native/3.29.0/jline-native-3.29.0.jar)
    # WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
    # WARNING: Restricted methods will be blocked in a future release unless native access is enabled
    # 
    # Welcome to Scala 3.7.1 (24.0.1, Java OpenJDK 64-Bit Server VM).
    # Type in expressions for evaluation. Or try :help.
    #                                                                                                                
    # scala> 
  • Scala 2.13
    scala-cli repl --jvm 24 -S 2.13
    # Welcome to Scala 2.13.16 (OpenJDK 64-Bit Server VM, Java 24.0.1).
    # Type in expressions for evaluation. Or try :help.
    # WARNING: A restricted method in java.lang.System has been called
    # WARNING: java.lang.System::load has been called by org.jline.nativ.JLineNativeLoader in an unnamed module (file:/Users/pchabelski/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/jline/jline/3.27.1/jline-3.27.1-jdk8.jar)
    # WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module
    # WARNING: Restricted methods will be blocked in a future release unless native access is enabled
    # 
    # 
    # scala>

After

  • Scala 3
    scala-cli repl --jvm 24
    # WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
    # WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ (file:/Users/pchabelski/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala3-library_3/3.7.1/scala3-library_3-3.7.1.jar)
    # WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
    # WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
    # Welcome to Scala 3.7.1 (24.0.1, Java OpenJDK 64-Bit Server VM).
    # Type in expressions for evaluation. Or try :help.
    #            
    #
    # scala> 
  • Scala 2.13
    scala-cli repl --jvm 24 -S 2.13
    # Welcome to Scala 2.13.16 (OpenJDK 64-Bit Server VM, Java 24.0.1).
    # Type in expressions for evaluation. Or try :help.
    # 
    # scala> 

Extra context

The remaining warning is tied to scala/scala3#9013 and will be addressed in Scala 3.8 (along with the JDK bump to 17)

cc @sjrd @lrytz

@Gedochao Gedochao requested a review from tgodzik June 27, 2025 12:46
@Gedochao Gedochao enabled auto-merge June 27, 2025 12:57
@Gedochao Gedochao merged commit 0bda9f0 into VirtusLab:main Jun 27, 2025
53 checks passed
@Gedochao Gedochao deleted the maintenance/repl-with-java-24 branch June 28, 2025 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JDK 24: REPL prints warning on startup about JLineNativeLoader
2 participants