Friday, January 4, 2019

Groovy 3.0.0 with JDK 10 on Windows 7+

How to run Groovy 3.0.0 with JDK 10 on Windows 7+?


Since the advent of the Java Module System, and the removal of the classes under the javax.xml.bind  package from the JDK, running Groovy became problematic under Java versions 9+. Groovy 3.0.0-alpha-3 installed on Windows 7 and using JDK 10.0.2 cannot run out of the box, instead we get this error message, running hello.groovy (containing only the print "Hello World" line)

λ groovy hello WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/C:/bin/Groovy/GROOVY~1.0/lib/groovy-3.0.0-alpha-3.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int) WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release Caught: java.lang.NoClassDefFoundError: Unable to load class groovy.xml.jaxb.JaxbGroovyMethods due to missing dependency javax/xml/bind/JAXB Context java.lang.NoClassDefFoundError: Unable to load class groovy.xml.jaxb.JaxbGroovyMethods due to missing dependency javax/xml/bind/JAXBContext         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)
Running a groovy script results in error.

Also groovy console can't run scripts:

groovy> print "Hello World"    Exception thrown  java.lang.NoClassDefFoundError: Unable to load class groovy.xml.jaxb.JaxbGroovyMethods due to missing dependency javax/xml/bind/Marshaller   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)
Groovy Console can't run scripts


The solution is easy, setting the  JAVA_TOOL_OPTIONS environment variable to
--add-modules=java.xml.bind will solve the problem. (System Properties dialog/ Advanced tab / Environment Variables button / Environment variables dialog / System variables list / New ... )

JAVA_TOOL_OPTIONS --add-modules=javax.xml.bind
Setting the system property JAVA_TOOL_OPTIONS to --add-modules=javax.xml.bind

Starting a console after this setting and running groovy scripts still gives a lot of warnings, but the scripts are run (note the "Hello World" at the bottom):

λ groovy hello Picked up JAVA_TOOL_OPTIONS: --add-modules=java.xml.bind WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/C:/bin/Groovy/GROOVY~1.0/lib/groovy-3.0.0-alpha-3.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int) WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1 WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations WARNING: All illegal access operations will be denied in a future release Hello World
The output of running the hello.groovy script after setting the JAVA_TOOL_OPTIONS environment variable

The Groovy Console is also working properly:

groovy> print "Hello World"    Hello World
Output of the Groovy Console after setting the JAVA_TOOL_OPTIONS environment variable


No comments:

Post a Comment