NoClassDefFoundError: groovy/lang/binding

classpath, groovy, jar, java

Solution

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 - the Java application launcher document

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

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

File structure

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

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

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.

Problem

I'm trying to evaluate Groovy script inside a Java app by using GroovyShell. Problem: My program compiles ok, but gives me a NoClassDefFoundError at run-time. 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 > jar cfm TestClass.jar Manifest.txt TestClass.class It compiles without error. Then I run it: > 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) ``` Full error text: http://puu.sh/2gOrx I've also tried running it with the same -cp as I compiled it, but it gives me the same error.

Original source

Related problems