How to invoke groovy with 'java' from command line

后端 未结 5 1324
萌比男神i
萌比男神i 2020-12-29 09:41

I have to ship some groovy code to some users that have only java installed (no grooy, no $groovy_home, etc). I\'m trying to invoke groovy from the commandline but I\'m havi

相关标签:
5条回答
  • 2020-12-29 10:25

    Watch out of [~]!

    java -cp .:~/path-to-groovy-all.jar YourClassName     # does not work
    java -cp ~/path-to-groovy-all.jar:. YourClassName     # works
    java -cp .:/full/path/to/goovy-all.jar YourClassName  # works
    

    In first line tilde is not processed by bash, and java can not understand it.

    In second line tilde is replaced by bash and everything works fine.

    0 讨论(0)
  • 2020-12-29 10:32

    One way to avoid problems with different class paths on different machines would be to bundle all the necessary dependencies into one single jar, this would also make distribution to users easier. This can be done with this 'GroovyWrapper' script. The default jars (embeddable/groovy-all-.jar and lib/commons.jar) are included by default in the script and if you require other JARS they can easily be added.

    See http://docs.codehaus.org/display/GROOVY/WrappingGroovyScript for the full script and instructions.

    Here's an example of how to use GroovyWrapper:

    Say you have groovy script HelloWorld.groovy, use GroovyWrapper for building HelloWorld.jar, as follows:

    $ groovy GroovyWrapper -c -m HelloWorld
    

    GroovyWrapper will compile the script HelloWorld.groovy to HelloWorld.class, and creates a self-executable jar HelloWorld.jar.

    Now you can use the HelloWorld.jar for launching the HelloWorld script, simply by running:

    $ java -jar HelloWorld.jar
    
    0 讨论(0)
  • 2020-12-29 10:33

    You have here another example of Groovy app called from Java (in this case, from ant, but the general idea is the same).

    java -cp [...];%GROOVY_HOME%/embeddable/groovy-all-1.5.4.jar;[..]
    

    As mentioned by frankowyer, you have the exact groovy jar explicitly listed on the classpath arguments of the java.

    Since your clients do not have special environment variable, just replace the %GROOVY_HOME%/... with the complete path to:

    • groovy.jar or
    • goovy-all-VERSION.jar (to minimize any conflict with other libraries)
    0 讨论(0)
  • 2020-12-29 10:41

    I think you need to explicitly list the groovy jar in the classpath

    0 讨论(0)
  • 2020-12-29 10:43

    GREAT ANSWER by VonC:

    ....... Since your clients do not have special environment variable, just replace the %GROOVY_HOME%/... with the complete path to:

    groovy.jar or
    goovy-all-VERSION.jar (to minimize any conflict with other libraries)........
    

    this is for windows Vista:

    In System Properties | Environmental Variables | User variables: Variable Name GROOVY_HOME set to C:\groovy\Groovy-1.8.5 and the Variable Name CLASSPATH value's set to include .;%GROOVY_HOME%\embeddable\groovy-all-1.8.5.jar

    Don't make the mistake I did(spinning my wheels a bit), of not prep-ending the path of the 'groovy-all' with ".;" (dot and semi-colon): as the path needs the base folder you're in for finding the main class(strange as it seems)

    0 讨论(0)
提交回复
热议问题