How to run a groovy code with java ? I have a simple groovy code: println "Hello World!"
After compiling code: groovy hello.groovy result hello.class
If i want to run the code with java, this byte cod don`t work java hello
Error: C:\>java hello Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) Caused by: java.lang.ClassNotFoundException: groovy.lang.Script at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) ... 12 more
If I classpath my lib like: java -classpath c:\lib c:\hello
Error:
C:\>java -classpath c:\lib\ c:\hello Exception in thread "main" java.lang.NoClassDefFoundError: c:\hello Caused by: java.lang.ClassNotFoundException: c:\hello at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Why don`t work ?
c:\lib is lib of groovy. c:\hello is a class hello world. [ July 07, 2008: Message edited by: Bro Fratello ]
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 32421
posted
0
Welcome to JavaRanch.
C:\>java -classpath c:\lib\ c:\hello
I don't know much about Groovy, but it probably needs to be
Replace GROOVY.JAR with the name of the Groovy library. Jar files need to be listed explicitly in the classpath; it's not sufficient to list their directory. [ July 07, 2008: Message edited by: Ulf Dittmer ]
Ulf is 99% right. You need more than the single groovy jar file. In one of the distributions there is a groovy-all.jar which can be used otherwise you'll need to have the classpath point to all the jar files under the $GROOVY_HOME/lib folder.