• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Calling a compiled groovy script from java interpreter

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm new to Groovy and trying to execute a very simple groovy script compiled to java class file. But I get the following error whenever I try to call it with groovy-all*.jar file location is given correctly. Can anyone please help me to locate the error with the command?

Hello.groovy: Content

Compiled using:
$ groovyc Hello.groovy //OK
$ echo $GROOVY_HOME
/home/indikau/opt/groovy-2.4.7 //OK
Error in the following
$ java -cp %GROOVY_HOME%/embeddable/groovy-all-2.4.7.jar;. Hello //ERROR!!!
//It just print -cp usage and then
bash: Hello: No such file or directory

 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Screenshot
 
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indika,
That is a script so it isn't compiling to a class. You could either wrap the Groovy in a class or write a Java program to call it. For the later:

File file = new File("Hello.groovy");
String[] args = new String[] { "Indika" };

GroovyShell shell = new GroovyShell();
shell.run(file, args);
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Indika,
That is a script so it isn't compiling to a class. You could either wrap the Groovy in a class or write a Java program to call it. For the later:

File file = new File("Hello.groovy");
String[] args = new String[] { "Indika" };

GroovyShell shell = new GroovyShell();
shell.run(file, args);



Thanks Jeanne,

But I've seen in this as an example on a Groovy book. It says groovyc compiled class can be executed as a java class file by including the groovy-all*.jar file with -ca path. If I try to run only the Hello.class it says no main method is declared. So the directions given as above to execute as above. The problem is with it does not find the Hello.class file even I specified it using . notation (after the .jar;.). I'm not sure if this correct with shell as well.

Indika.
 
Jeanne Boyarsky
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did it say a groovy class or a groovy script? I've done with groovy classes. I've never seen it done this way for a script.
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the original code listing from the book.

Listing 2-1.  A Simple Groovy Script: Hello.groovy


Listing 2-4.  Compiling Groovy with groovyc
groovyc Hello.groovy
As you might expect, compiling Hello. Groovy results in Hello.class. Because Groovy compiles to Java bytecode,
you can use the Java command line to execute it.

Listing 2-5.  Running the Groovy Program Using Java
java -cp %GROOVY_HOME%/embeddable/groovy-all-2.0.0.jar;. Hello "Luke Skywalker"
Here is the output:
Hello Luke Skywalker

 
Jeanne Boyarsky
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. Good to know!
 
Saloon Keeper
Posts: 7094
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're not using Windows, you need to use a colon in the classpath, not a semicolon.
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:Since you're not using Windows, you need to use a colon in the classpath, not a semicolon.



Tried. But no luck.

indikau@ruhuna ~/Documents/beginning_groovy $ pwd
/home/indikau/Documents/beginning_groovy
indikau@ruhuna ~/Documents/beginning_groovy $ java -cp %GROOVY_HOME%/embeddable/groovy-all-2.4.7.jar:. SayHello indika
Error: Could not find or load main class SayHello
indikau@ruhuna ~/Documents/beginning_groovy $
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This seems it can not find the main now. Is this code actually possible?
 
Jeanne Boyarsky
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
%GROOVY_HOME% is Windows too. On UNIX, that should be $GROOVY_HOME
 
Indika Rajapaksha
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:%GROOVY_HOME% is Windows too. On UNIX, that should be $GROOVY_HOME



Thanks a lot Jeanne. It worked.

 
Jeanne Boyarsky
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent. And I've learned that you can run a standalone Groovy script that way!
reply
    Bookmark Topic Watch Topic
  • New Topic