在groovy的官方文档中,groovy3是最高版本,最高支持到jdk1.8,如果使用jdk10的话,安装groovy后或出现警告:
WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/C:/Program Files (x86)/Groovy/Groovy-3.0.0/lib/groovy-3.0.0-alpha-4.jar) to method java.lang.Object.finalize() WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass 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 Groovy Version: 3.0.0-alpha-4 JVM: 10.0.2 Vendor: "Oracle Corporation" OS: Windows 10在cmd中打开groovy文件:
我在使用jdk10中,使用idea建一个groovy的项目,启动后直接报错
Information:Groovyc: While compiling jdk10GroovyDemo2:java.lang.NoClassDefFoundError: Unable to load class org.apache.groovy.jaxb.extensions.JaxbExtensions due to missing dependency javax/xml/bind/JAXBContext at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:408) at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:281) at org.codehaus.groovy.ast.ClassNode.getMethods(ClassNode.java:406) at org.codehaus.groovy.macro.transform.MacroMethodsCache.scanExtClasses(MacroMethodsCache.java:88) at org.codehaus.groovy.macro.transform.MacroMethodsCache.access$000(MacroMethodsCache.java:45) at org.codehaus.groovy.macro.transform.MacroMethodsCache$2.onModule(MacroMethodsCache.java:69) at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromProperties(ExtensionModuleScanner.java:87) at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanExtensionModuleFromMetaInf(ExtensionModuleScanner.java:81) at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModulesFrom(ExtensionModuleScanner.java:63) at org.codehaus.groovy.runtime.m12n.ExtensionModuleScanner.scanClasspathModules(ExtensionModuleScanner.java:54) at org.codehaus.groovy.macro.transform.MacroMethodsCache.getMacroMethodsFromClassLoader(MacroMethodsCache.java:76) at org.codehaus.groovy.macro.transform.MacroMethodsCache$1.provide(MacroMethodsCache.java:53) at org.codehaus.groovy.macro.transform.MacroMethodsCache$1.provide(MacroMethodsCache.java:50) at org.codehaus.groovy.runtime.memoize.StampedCommonCache.compute(StampedCommonCache.java:163) at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:154) at org.codehaus.groovy.runtime.memoize.StampedCommonCache.getAndPut(StampedCommonCache.java:115) at org.codehaus.groovy.macro.transform.MacroMethodsCache.get(MacroMethodsCache.java:50) at org.codehaus.groovy.macro.transform.MacroCallTransformingVisitor.findMacroMethods(MacroCallTransformingVisitor.java:117) at org.codehaus.groovy.macro.transform.MacroCallTransformingVisitor.visitMethodCallExpression(MacroCallTransformingVisitor.java:88) at org.codehaus.groovy.ast.expr.MethodCallExpression.visit(MethodCallExpression.java:70) at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:119) at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:197) at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42) at org.codehaus.groovy.ast.MethodCallTransformation.visit(MethodCallTransformation.java:55) at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:320) at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:976) at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:651) at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:627) at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:604) at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:62) at org.jetbrains.groovy.compiler.rt.DependentGroovycRunner.runGroovyc(DependentGroovycRunner.java:115) 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:564) at org.jetbrains.groovy.compiler.rt.GroovycRunner.intMain2(GroovycRunner.java:136) 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:564) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.runGroovycInThisProcess(InProcessGroovyc.java:158) at org.jetbrains.jps.incremental.groovy.InProcessGroovyc.lambda$runGroovyc$0(InProcessGroovyc.java:88) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:844)在查遍官网之后和百度之后,在外网中找到了几个办法,
T0(降低groovy的版本)
groovy之前有个分支,是2.4版本系列,在这个版本中,2.4.15以后的版本都是支持JDK 9/10,如果只是学习的话,可以下在这版本进行更改,(这个版本的很多功能都不太全,如果要有顾虑的话,请参考下面答案)
地址:
https://dl.bintray.com/groovy/Distributions/groovy-2.4.17-installer.exe
1,降版本
JAXB API被认为是Java EE API,因此不再包含在Java SE 9中的默认类路径中。在Java 11中,它们完全从JDK中删除。
Java 9引入了模块的概念,默认情况下,java.se聚合模块在类路径(或更确切地说,模块路径)上可用。正如其名称所暗示的,java.se汇聚模块并没有包括那些与Java 6/7/8传统上捆绑了Java EE的API。
所以,大家只要将jdk版本降低到1.8以下,就可以正常执行。
2,直接绑定路径(仅限JDK 9/10)
幸运的是,JDK 6/7/8中提供的这些Java EE API仍然在JDK中,但它们默认情况下不在类路径上。以下模块中提供了额外的Java EE API:
java.activation java.corba java.transaction java.xml.bind << This one contains the JAXB APIs java.xml.ws java.xml.ws.annotation快速而肮脏的解决方案:(仅限JDK 9/10) 要在运行时使JAXB API可用,请指定以下命令行选项
您可以使用--add-modules=java.xml.bindJVM选项将xml绑定模块添加到JVM运行时环境。
例如: java --add-modules=java.xml.bind XmlTestClass
如果您尝试--add-modules使用较旧的JDK进行指定,那么它将会爆炸,因为它是一个无法识别的选项。我建议两种选择之一:
您可以通过检查检查JDK版本有条件地适用参数的启动脚本(如果有的话)$JAVA_HOME/release的JAVA_VERSION属性。您可以添加-XX:+IgnoreUnrecognizedVMOptions以使JVM静默忽略无法识别的选项,而不是炸毁。但要小心!您使用的任何其他命令行参数将不再由JVM验证。此选项适用于Oracle / OpenJDK以及IBM JDK(从JDK 8sr4开始)备用快速解决方案:(仅限JDK 9/10) 请注意,您可以通过指定--add-modules java.se.ee选项在运行时使所有上述Java EE模块可用。该java.se.ee模块是一个聚合模块,包括java.se.ee以上Java EE API模块。
3,适当的长期解决方案:(JDK 9及更高版本)
上面列出的Java EE API模块都已标记@Deprecated(forRemoval=true),因为它们计划在Java 11中删除。因此,该--add-module方法将不再适用于Java 11开箱即用。
您需要在Java 11和转发中执行的操作是在类路径或模块路径上包含您自己的Java EE API副本。例如,您可以将JAX-B API添加为maven依赖项,如下所示:
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.1</version> </dependency>请注意,从版本2.3.1开始,您不再需要添加javax.activation。(见https://github.com/eclipse-ee4j/jaxb-ri/issues/1222)
请注意,com.sun.xml.bind工件是旧的,仅提供向后兼容性。您应该使用等效的org.glassfish.jaxb工件,如其他一些答案中所述
对于Android Studio开发人员:(JDK 9及更高版本)
您可能想知道在哪里使用此解决方案。
sourceCompatibility = 10 // You can also decrease your souce compatibility to 1.8 //java 9+ does not have Jax B Dependents compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.0' compile group: 'com.sun.xml.bind', name: 'jaxb-core', version: '2.3.0' compile group: 'com.sun.xml.bind', name: 'jaxb-impl', version: '2.3.0' compile group: 'javax.activation', name: 'activation', version: '1.1.1'
个人建议试试这个:
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency>刷新之后:
在jdk12中,WARNING依然存在,不过可以正常使用了,
不过在eclipse中,出现了一个警告:
Java HotSpot(TM) 64-Bit Server VM warning: Archived non-system classes are disabled because the java.system.class.loader property is specified (value = "groovy.lang.GroovyClassLoader"). To use archived non-system classes, this property must not be set WARNING: An illegal reflective access operation has occurred WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/D:/Eclipse/plugins/org.codehaus.groovy_3.0.0.xx-201903290242-e1903-RELEASE/lib/groovy-3.0.0-indy.jar) to method java.lang.Object.finalize() WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass 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
多出来了一行:
Java HotSpot(TM) 64-Bit Server VM warning: Archived non-system classes are disabled because the java.system.class.loader property is specified (value = "groovy.lang.GroovyClassLoader"). To use archived non-system classes, this property must not be set ,个人猜测在jdk12中,eclipse版本没更上,出现了这个,不过问题不大,
参考网址:(很卡,)
https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j/46455026#
需要jar包的在此下载后可以直接加入到项目中,即可正常使用groovy。
..https://download.csdn.net/download/qq_41886200/11193577