0

Trying to get the following script:

class MyClass {
  static void main(String... args) {
    println "Hello ${args[0]}"
  }
}

To run like Java with: java MyTest John for example.

Reading the groovy docs on scripts with main I was under the impression I could to the following to achieve my goal:

  1. Run groovyc MyTest.groovy
  2. Run java MyTest John

I was even under the impression that I could leave just the body of the main function and still be able to compile until a class that extends from Script...

I can run the script with groovy MyTest.groovy John and (after compiling with groovyc) groovy MyTest John

How can I accomplish my goal? What am I doing wrong?

1 Answer 1

1

You need groovy on the classpath when you run the java command, ie:

Save this as MyClass.groovy

class MyClass {
  static main(args) {
    println "Hello ${args[0]}"
  }
}

Then run:

groovyc MyClass.groovy

Then run:

java -cp $GROOVY_HOME/embeddable/groovy-all-2.4.4.jar:. MyClass John
Sign up to request clarification or add additional context in comments.

2 Comments

Crazy how 1 year old tutorials are already outdated on that matter. Most of them point to -cp $GROOVY_HOME/lib or don't point to anything at all.
You can get the individual jars from lib, but I tend to point to all as they I don't have to think which jars I might need ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.