NoClassDefFoundError:groovy / lang / binding [英] NoClassDefFoundError: groovy/lang/binding

查看:222
本文介绍了NoClassDefFoundError:groovy / lang / binding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GroovyShell评估Java应用程序中的Groovy脚本。

I'm trying to evaluate Groovy script inside a Java app by using GroovyShell.

问题:我的程序可以编译,但是在运行时给我一个NoClassDefFoundError。

Problem: My program compiles ok, but gives me a NoClassDefFoundError at run-time.

TestClass.java:

TestClass.java:

import groovy.lang.Binding;
import groovy.lang.GroovyShell;

class TestClass {
    static Binding binding;
    static GroovyShell shell;

    public static void main(String[] args) {
        System.out.println("Hello, world.");
        binding = new Binding();
        shell = new GroovyShell(binding);
        Object value = shell.evaluate("5 ** 5");
    }
}

然后我用以下命令编译:

Then I compile with:


> javac -cp%GROOVY_HOME%\embeddable\groovy-all-2.1.1.jar TestClass.Java

> javac -cp %GROOVY_HOME%\embeddable\groovy-all-2.1.1.jar TestClass.Java

> jar cfm TestClass.jar Manifest.txt TestClass.class

> jar cfm TestClass.jar Manifest.txt TestClass.class

它编译时没有错误。然后运行它:

It compiles without error. Then I run it:


> java -jar TestClass.jar

> java -jar TestClass.jar


Hello, world
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Binding
    at TestClass.main(TestClass.java:10)
Caused by: java.lang.ClassNotFoundException: groovy.lang.Binding
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)

完整错误文本: http://puu.sh/2gOrx

我也尝试使用相同的命令运行它- cp编译时,却给了我同样的错误。

I've also tried running it with the same -cp as I compiled it, but it gives me the same error.

推荐答案

全局类路径,例如CLASSPATH环境和- cp选项,如果您正在运行可执行JAR,则不会生效。请参阅此帖子以获取详细信息: java -jar选项是否会更改类路径选项

Global classpath, for example, CLASSPATH environment and "-cp" option, won't take affect if you are running an executable JAR. Please refer to this post for details: Does java -jar option alter classpath options.

java-Java应用程序启动器文档


使用此选项时,JAR文件是所有用户类和其他用户类路径设置的源会被忽略。

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

相反,您需要在清单文件中设置Class-Path。检查以下示例。

Instead, you need to set Class-Path in manifest file. Check the following sample.

文件结构

|-- Manifest.txt
|-- TestClass.class
|-- TestClass.jar
|-- TestClass.java
`-- lib
    `-- groovy-all-2.1.1.jar

Manifest.txt(不要忘了添加以结尾的新行最后一行)

Manifest.txt (don't forget to add a new line ending for last line)

Main-Class: TestClass
Class-Path: lib/groovy-all-2.1.1.jar

然后在问题中执行相同的命令来生成并运行可执行的JAR。有关更多信息,请访问以下Wiki页面:在清单文件中设置路径

And execute the same commands in your question to generate and run an executable JAR. For more information, check this wiki page: Setting the path in a Manifest file.

这篇关于NoClassDefFoundError:groovy / lang / binding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆