[英]When running Groovy scripts using the groovy-all jar, how do you specify a classpath?
I've found an example for running Groovy scripts on systems that do not have Groovy installed using the groovy-all jar file. 我找到了一个在没有使用groovy-all jar文件安装Groovy的系统上运行Groovy脚本的示例。 I attempted the following:
我尝试了以下方法:
java -cp src:.:lib/* -jar lib/groovy-all-2.0.1.jar src/com/example/MyScript.groovy
The trouble is my script depends on jars in the lib directory plus two other Groovy script files located in src/com/examples. 问题是我的脚本依赖于lib目录中的jar以及位于src / com / examples中的另外两个Groovy脚本文件。 When I run this, it complains about the import statements for all of them.
当我运行它时,它会抱怨所有这些的import语句。 I can run it on a system that has Groovy installed fine by using the following:
我可以通过使用以下方法在安装了Groovy的系统上运行它:
CLASSPATH="src:.:lib/*" groovy src/com/example/MyScript.groovy
How do I run Groovy scripts this way, using the groovy-all jar, as well as give it a classpath? 如何使用groovy-all jar以这种方式运行Groovy脚本,并为其提供类路径?
You can't combine both -jar
and -cp
in a java
command, so you need to name the main class explicitly. 您不能在
java
命令中同时使用-jar
和-cp
,因此需要显式命名主类。 Looking at the manifest of the groovy-all
JAR, the main class name is groovy.ui.GroovyMain
, so you need 看着
groovy-all
JAR的清单,主要的类名是groovy.ui.GroovyMain
,所以你需要
java -cp 'src:.:lib/*' groovy.ui.GroovyMain src/com/example/MyScript.groovy
(if groovy-all were not already covered by lib/*
you would need to add that to the -cp
as well). (如果groovy-all还没有被
lib/*
覆盖,你还需要将它添加到-cp
中)。
One way is to compile the Groovy files to "*.class" files. 一种方法是将Groovy文件编译为“* .class”文件。 Then include the jar from the $GROOVY_HOME/embeddable directory and put it on the classpath.
然后包含$ GROOVY_HOME / embeddable目录中的jar并将其放在类路径中。
Here is a minimalist example (the first line is a simple Unix copy; use whatever works for you): 这是一个极简主义的例子(第一行是一个简单的Unix副本;使用适用于你的任何东西):
$ cp /some-dir/groovy-1.8.5/embeddable/* .
$ groovyc Test.groovy
$ java -cp . groovy-all-1.8.5.jar Test
For typical distribution, you would use Ant/Maven/Gradle to build your own jar file with the compiled Groovy (ie class files) in it. 对于典型的发行版,您可以使用Ant / Maven / Gradle构建自己的jar文件,其中包含已编译的Groovy(即类文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.