我是Groovy的新手,曾尝试使用GStringTemplateEngine在JBoss 5.1 上执行一些Groovy脚本
在我的本地开发环境中一切都很好,但当转移到由另一个团队管理的开发服务器时会失败。
我发现异常是专门针对抛出的
try {
groovyClass = loader.parseClass(new GroovyCodeSource(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy", "x"));
} catch (Exception e) {
throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}
在GStringTemplateEngine.class上,第190行
异常消息为
groovy.lang.GroovyRuntimeException: startup failed:
General error during class generation: URI is not hierarchical
java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.<init>(File.java:363)
at org.jboss.net.protocol.file.FileURLConnection.<init>(FileURLConnection.java:62)
at org.jboss.net.protocol.file.Handler.openConnection(Handler.java:40)
at java.net.URL.openConnection(URL.java:945)
at java.net.URLClassLoader.getPermissions(URLClassLoader.java:474)
at groovy.lang.GroovyClassLoader.getPermissions(GroovyClassLoader.java:335)
at java.security.SecureClassLoader.getProtectionDomain(SecureClassLoader.java:235)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at groovy.lang.GroovyClassLoader.access$300(GroovyClassLoader.java:55)
at groovy.lang.GroovyClassLoader$ClassCollector.createClass(GroovyClassLoader.java:475)
at groovy.lang.GroovyClassLoader$ClassCollector.onClassNode(GroovyClassLoader.java:492)
at groovy.lang.GroovyClassLoader$ClassCollector.call(GroovyClassLoader.java:496)
at org.codehaus.groovy.control.CompilationUnit$14.call(CompilationUnit.java:792)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1024)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:562)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:540)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:517)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:283)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:260)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)
我不知道为什么它抛出了一个URI而不是层次错误。我假设它与权限有关,GroovyClassLoader无法解析生成的类。
以前有人见过这个错误吗?如果有人能就调试/修复问题提供一些建议,那将是很有帮助的。
不确定问题的真正原因,但找到了解决方法。
创建了GStringTemplateEngine的副本并替换了
try {
groovyClass = loader.parseClass(new GroovyCodeSource(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy", "x"));
} catch (Exception e) {
throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}
带有
try {
groovyClass = loader.parseClass(templateExpressions.toString(), "GStringTemplateScript" + counter.incrementAndGet() + ".groovy");
} catch (Exception e) {
throw new GroovyRuntimeException("Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): " + e.getMessage());
}
删除了GroovyCodeSource,并将模板字符串传递给parseClass方法。