简体   繁体   English

使用groovy-all jar运行Groovy脚本时,如何指定类路径?

[英]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.

相关问题 在OSGi中运行时,如何将groovy-all从2.4升级到2.5? - How to upgrade groovy-all from 2.4 to 2.5 when running in OSGi? 运行 javac 命令时如何指定类路径? - How do you specify the classpath when running the javac command? 如何显示Groovy AntBuilder类路径中包含的所有资源? - How to display all resources included in a Groovy AntBuilder classpath? eclipse 插件中存在的“groovy-all”jar 与 maven 依赖项之间的兼容性问题 - Compatibility issue between the 'groovy-all' jars present in eclipse plugin and maven dependency 运行代码为 a.jar 时出现 groovy.lang.missingMethodException - groovy.lang.missingMethodException when running code as a .jar 如何使用maven-jar-plugin和groovy构建可执行jar? - How to build executable jar using maven-jar-plugin and groovy? 如何在jar清单类路径中为属性文件指定目录? - How do I specify a directory for properties files in a jar manifest classpath? 如何从命令行在类路径中指定mysql jar? - How do I specify the mysql jar in the classpath from the command line? 如何在 jar 中包含 Groovy 扩展方法? - How do I include Groovy Extension methods in jar? 如何从命令行运行Groovy脚本作为Java? - How do I run Groovy scripts as Java, from the command line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM