jQuery in Action, 2nd edition
The moose likes Groovy and the fly likes Groovy Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Other Languages » Groovy
Reply Bookmark "Groovy" Watch "Groovy" New topic
Author

Groovy

Stoian Ovidiu
Greenhorn

Joined: Jul 07, 2008
Posts: 6
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
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 ]

Android appsImageJ pluginsJava web charts
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15020

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.


My Blog | DZone Articles
Stoian Ovidiu
Greenhorn

Joined: Jul 07, 2008
Posts: 6
many thanks.
Now works.
java -classpath c:\groovy.jar;c:\lib hello
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15020

Bro - Please check your private messages.

Thanks.
Stoian Ovidiu
Greenhorn

Joined: Jul 07, 2008
Posts: 6
Ok
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15020

Originally posted by Stoian Ovidiu:
Ok


You the man! Thanks Stoian!
 
 
subject: Groovy
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture