我尝试在安装了Java 10的服务器上运行此Groovy脚本(在Java 8上可以正常运行)。
@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" })
}
}
但我总是会收到一个缺少依赖项的错误提示:
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)
--add-modules java.xml.bind
参数运行Java 9或10运行时。有很多关于此的解释,例如在这里https://dev59.com/TFcP5IYBdhLWcg3w8uhi。 - Alex Pakka