4

I try to run this groovy script (which runs fine on java 8) on a server which has java 10 installed.

@Grapes([
        @Grab(group='org.slf4j', module='slf4j-api', version='1.7.25'),
        @Grab(group='ch.qos.logback', module='logback-classic', version='1.2.3'),
        @Grab(group='org.apache.commons', module='commons-lang3', version='3.7'),
        @Grab(group='com.sparkjava', module='spark-core', version='2.7.2'),
        @Grab(group='javax.xml.bind', module='jaxb-api', version='2.3.0'),
        @Grab(group='com.sun.xml.bind', module='jaxb-core', version='2.3.0'),
        @Grab(group='com.sun.xml.bind', module='jaxb-impl', version='2.3.0')
])
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.util.logging.Slf4j
import org.apache.commons.lang3.exception.ExceptionUtils
import spark.Route
import static spark.Spark.*

@Slf4j
class Server {
    public static void main(String[] args) {
        int serverPort = args.length > 0 ? args[0].toInteger() : 1001
        log.info("start server at $serverPort ... ")
        port(serverPort)

        get("/hello", { req, rep -> "world" })
    }
}

But I always get a dependency missing error:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/opt/groovy-2.5.0-rc-3/lib/groovy-2.5.0-rc-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/JAXBContext
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) 
KIC
  • 5,887
  • 7
  • 58
  • 98
  • Have you considered raising this on the mailing list or submitting a bug to the Jira? Might be able to get a quicker response with this, and it might be fixable before the final 2.5 release? – tim_yates May 26 '18 at 11:09
  • 1
    Try running java 9 or 10 runtime with `--add-modules java.xml.bind ` parameter. There are plenty of explanations for this, e.g. here https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j – Alex Pakka May 27 '18 at 04:28
  • 1
    Possible duplicate of: https://stackoverflow.com/questions/49291479/noclassdeffounderror-exception-while-running-basic-groovy-script-from-console – ZhekaKozlov May 27 '18 at 15:47

2 Answers2

0

Looking at JEP 320 we know that some of Java EE modules has been removed:

Remove the Java EE and CORBA modules from the Java SE Platform and the JDK

The stacktrace talks about JAXB is part of Java EE Platform. I suggest using Groovy v3.x which support JDK 9/10.

Hope this helps.

Ibrahim.H
  • 1,062
  • 1
  • 13
  • 22
0

Judging from logs you were at groovy version 2.5.0-rc-3.

Upgrading to >= 2.5.1 will fix that issue.

Note: there's no need to import javax.xml.bind

lukaszrys
  • 1,656
  • 4
  • 19
  • 33
  • WARNING: All illegal access operations will be denied in a future release Groovy Version: 2.5.2 JVM: 10.0.2 Vendor: "Oracle Corporation" OS: Linux – DimiDak Mar 14 '19 at 12:14