Question

I am very new to groovy and I am trying out this example from the Groovy in Action book. I have this fibonacci.groovy program and when trying to run the program with java command, I am getting the NoClassDefFound error.

The command I am using in the console is:

java -cp %GROOVY_HOME%/embeddable/groovy-all-2.2.0.jar;classes fibonacci

As you can see, I have mentioned the groovy-all jar in the classpath and I set the GROOVY_HOME variable. The classpath variable is not set, so I am assuming it has the default '.' value to find in the current folder itself. What am I doing wrong?

Was it helpful?

Solution

Aren't you missing the current folder in the classpath?

I'm on Linux, but if i compile a Groovy class with groovyc and then try to run it with java, i need to tell java where is my groovy-all.jar and also add the current dir to the classpath

So, this compilation works:

$ groovyc Fib.groovy

But this run doesn't runs:

$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:classes Fib

As it's missing the current dir in the path:

$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. Fib
test for fib

Also note that if fibonacci is in a package, you need to type the full path to the class. So for this groovy source:

package up.foo
println "test for fib"

Compile:

$ groovyc Fib.groovy

We write the full package path to execute:

$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. up.foo.Fib

There it is:

test for fib
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top