英文:
GroovyShell and the classpath
问题
我开始使用Groovy进行编程,我想知道当我使用GroovyShell(new File("script.groovy").text)。evaluate()时,脚本类是否可以在类路径中看到依赖项,我尝试了这个例子,但产生了以下错误。
// main.groovy
@Grapes([
@Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
@Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])
import groovy.lang.GroovyShell;
import java.io.File;
new GroovyShell().evaluate(new File("C:\\Users\\Dev\\Desktop\\logger.groovy").text);
// logger.groovy
import org.slf4j.*
import groovy.util.logging.Slf4j
@Slf4j
class HelloWorldSlf4j {
def execute() {
log.debug 'Execute HelloWorld.'
log.info 'Simple sample to show log field is injected.'
}
}
def helloWorld = new HelloWorldSlf4j()
helloWorld.execute()
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:869)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:979)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:967)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:346)
... 38 more
英文:
I'm starting to program with groovy and I wanted to know when I use GroovyShell (new File ("script.groovy"). txt) .evaluate (),
do script classes see dependencies in classpath or not, i try this example and it generate the following errors.
// main.groovy
@Grapes([
@Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
@Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])
import groovy.lang.GroovyShell;
import java.io.File;
new GroovyShell().evaluate(new File("C:\\Users\\Dev\\Desktop\\logger.groovy").text);
// logger.groovy
import org.slf4j.*
import groovy.util.logging.Slf4j
@Slf4j
class HelloWorldSlf4j {
def execute() {
log.debug 'Execute HelloWorld.'
log.info 'Simple sample to show log field is injected.'
}
}
def helloWorld = new HelloWorldSlf4j()
helloWorld.execute()
Caused by: java.lang.ClassNotFoundException: org.slf4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:869)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:979)
at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:967)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.codehaus.groovy.transform.LogASTTransformation$AbstractLoggingStrategy.classNode(LogASTTransformation.java:346)
... 38 more
答案1
得分: 2
如果您希望 `logger.groovy` 文件与 `main.groovy` 脚本使用相同的类加载器,您需要通过 `GroovyShell` 构造函数将其传递。您可以通过调用 `this.class.classLoader` 来获取当前的类加载器。
<!-- language: lang-groovy -->
@Grapes([
@Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
@Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])
import groovy.lang.GroovyShell
import java.io.File
new GroovyShell(this.class.classLoader).evaluate(new File("logger.groovy"))
输出:
$ groovy main.groovy
16:17:11.325 [main] DEBUG HelloWorldSlf4j - 执行 HelloWorld。
16:17:11.327 [main] INFO HelloWorldSlf4j - 简单示例,显示日志字段已注入。
英文:
If you want logger.groovy
file to use the same classloader as main.groovy
script, you need to pass it with the GroovyShell
constructor. You can get the current classloader by calling this.class.classLoader
.
<!-- language: lang-groovy -->
@Grapes([
@Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
@Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
])
import groovy.lang.GroovyShell
import java.io.File
new GroovyShell(this.class.classLoader).evaluate(new File("logger.groovy"))
Output:
$ groovy main.groovy
16:17:11.325 [main] DEBUG HelloWorldSlf4j - Execute HelloWorld.
16:17:11.327 [main] INFO HelloWorldSlf4j - Simple sample to show log field is injected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论