简体   繁体   中英

How to run Groovy script with java command?

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM