如何从命令行使用“java”调用 groovy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/378905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:09:35  来源:igfitidea点击:

How to invoke groovy with 'java' from command line

javacommand-linegroovy

提问by Miguel Ping

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 having no luck. Here's my bat file:

我必须将一些 groovy 代码发送给一些只安装了 java 的用户(没有 grooy,没有 $groovy_home 等)。我正在尝试从命令行调用 groovy,但我没有运气。这是我的bat文件:

java -classpath .;lib;bin;bin-groovy introspector.AclCollector

And here's my exception:

这是我的例外:

Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
       at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access
java -cp [...];%GROOVY_HOME%/embeddable/groovy-all-1.5.4.jar;[..]
0(URLClassLoader.java:56) at java.net.URLClassLoader.run(URLClassLoader.java:195) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject at java.net.URLClassLoader.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:307) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:252) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320) ... 12 more Could not find the main class: introspector.AclCollector. Program will exit.

Does anyone have a clue? I have 'groovy-all-1.6-beta-1.jar' in \lib dir.

有人有线索吗?我在 \lib 目录中有“groovy-all-1.6-beta-1.jar”。

采纳答案by frankodwyer

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

我认为您需要在类路径中明确列出 groovy jar

回答by VonC

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

你必须在这里从Java称为Groovy中应用的另一个例子(在这种情况下,蚂蚁,但总的想法是一样的)。

$ groovy GroovyWrapper -c -m HelloWorld

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

正如 frankowyer 所提到的,您在 java.util.Date 的类路径参数中明确列出了确切的 groovy jar。

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

由于您的客户端没有特殊的环境变量,只需将 替换%GROOVY_HOME%/...为完整路径:

回答by Chris

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.

避免不同机器上不同类路径出现问题的一种方法是将所有必要的依赖项捆绑到一个 jar 中,这也将使分发给用户更容易。这可以通过这个“GroovyWrapper”脚本来完成。默认情况下,脚本中包含默认 jar(embeddable/groovy-all- .jar 和 lib/commons.jar),如果您需要其他 JARS,可以轻松添加它们。

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

有关完整脚本和说明,请参阅http://docs.codehaus.org/display/GROOVY/WrappingGroovyScript

Here's an example of how to use GroovyWrapper:

以下是如何使用 GroovyWrapper 的示例:

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

假设你有一个 groovy 脚本 HelloWorld.groovy,使用 GroovyWrapper 来构建 HelloWorld.jar,如下:

$ java -jar HelloWorld.jar

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

GroovyWrapper 会将脚本 HelloWorld.groovy 编译为 HelloWorld.class,并创建一个自执行 jar HelloWorld.jar。

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

现在您可以使用 HelloWorld.jar 来启动 HelloWorld 脚本,只需运行:

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

回答by Rich Wagner

GREAT ANSWER by VonC:

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)........

....... 由于您的客户端没有特殊的环境变量,只需将%GROOVY_HOME%/...替换为完整路径:

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

this is for windows Vista:

这适用于 Windows Vista:

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

在系统属性 | 环境变量 | 用户变量:变量名GROOVY_HOME设置为C:\groovy\Groovy-1.8.5,变量名CLASSPATH值设置为包括.;%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)

不要犯我犯过的错误(稍微转动一下我的轮子),没有用“.;”预先结束'groovy-all'的路径 (点和分号):因为路径需要您所在的基本文件夹才能找到主类(看起来很奇怪)

回答by Sam Sol

Watch out of [~]!

小心[~]!

##代码##

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

在第一行波浪号不被 bash 处理,java 无法理解它。

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

在第二行中,波浪号被 bash 取代,一切正常。