Skip to content
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

fix for #13760 - running script compiled jar no longer restricted to java.base module #15103

Merged
merged 2 commits into from
May 19, 2022
Merged

fix for #13760 - running script compiled jar no longer restricted to java.base module #15103

merged 2 commits into from
May 19, 2022

Conversation

philwalk
Copy link
Contributor

@philwalk philwalk commented May 4, 2022

This resolves the problem described in #13760.

The essence of the problem: running a script with the -save option will throw a ClassNotFoundException, but only when running from the compiled jar, and only if the script references modules other than java.base . The jar is fine when run via java -jar <jarfile>, the issue only occurs when scala launches the jar.

The workaround for the problem has been to append the -nosave option to the hashbang line.

The cause of the problem is that bootClassLoader in ScalaClassLoader is set to null when running in jdk9+.
The following command line illustrates the problem code at ScalaClassLoader line 70:

$ /opt/scala3/bin/scala -e 'import java.lang.invoke.*; MethodHandles.lookup().findStatic(classOf[ClassLoader], "getPlatformClassLoader", MethodType.methodType(classOf[ClassLoader])).invoke().asInstanceOf[ClassLoader]'
```scala
$ /opt/scala3/bin/scala -e 'import java.lang.invoke.*; MethodHandles.lookup().findStatic(classOf[ClassLoader], "getPlatformClassLoader", MethodType.methodType(classOf[ClassLoader])).invoke().asInstan
ceOf[ClassLoader]'
Exception in thread "main" java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle()ClassLoader to (Object[])Object
        at java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:861)
        at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:847)
        at java.base/java.lang.invoke.MethodHandleImpl$WrappedMember.asTypeUncached(MethodHandleImpl.java:1371)
        at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:847)
        at java.base/java.lang.invoke.Invokers.checkGenericType(Invokers.java:495)
        at compileFromString$minus08906c21$minusee54$minus45f1$minusb470$minusfe08bfbca561$package$.main(compileFromString-08906c21-ee54-45f1-b470-fe08bfbca561.scala:2)
        at main.main(compileFromString-08906c21-ee54-45f1-b470-fe08bfbca561.scala:1)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at dotty.tools.scripting.StringDriver.compileAndRun(StringDriver.scala:36)
        at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:257)
        at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:267)
        at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
```

An explanation of why this code no longer works for jdk9+ is described here: Regression in behavior of a null parent ClassLoader

The proposed fix is based on the fix to #11658.

@philwalk philwalk requested a review from dwijnand May 4, 2022 13:40
@philwalk philwalk changed the title fix for #13760 fix for #13760 - scripts running from compiled jar can only reference java.base module May 6, 2022
@philwalk philwalk changed the title fix for #13760 - scripts running from compiled jar can only reference java.base module fix for #13760 - running script compiled jar no longer restricted to java.base module May 6, 2022
@philwalk philwalk requested a review from som-snytt May 6, 2022 14:49
@bishabosha bishabosha merged commit 1390609 into scala:main May 19, 2022
@Kordyjan Kordyjan added this to the 3.2.0 milestone Aug 2, 2023
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.

script with java.sql.Date reference hits a NoClassDefFoundError when running from compiled jar
3 participants