java 在 Ant 脚本中将命令行参数传递给 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1965055/
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 18:44:34  来源:igfitidea点击:

Pass a command line argument to JAR in an Ant script

javaant

提问by Mark Lakewood

I've got a executable JARfile. And I've got an Antbuild script that compiles and then creates this JAR file. I would like a task to run the JAR file as well, but I've got a command line argument that needs to be passed to the JAR. It's a configuration file. The run target is below

我有一个可执行的JAR文件。我有一个Ant构建脚本,可以编译然后创建这个 JAR 文件。我也想要一个运行 JAR 文件的任务,但我有一个需要传递给 JAR 的命令行参数。这是一个配置文件。运行目标如下

<target name="run">
    <java jar="build/jar/ShoutGen.jar" fork="true"/>
    <arg line="/home/munderwo/workspace/ShoutGen-Java/ShoutGen.conf"/>
</target>

When I try to do this and run it from within Eclipse I get

当我尝试这样做并从 Eclipse 中运行它时,我得到

Buildfile: /home/munderwo/workspace/ShoutGen-Java/build.xml
run:
     [java] No config file passed as an argument. Please pass a configuration file
     [java] Java Result: 16

BUILD FAILED
/home/munderwo/workspace/ShoutGen-Java/build.xml:24: Problem: failed to create task or type arg
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

The error output from Java is my coded error meaning "you didn't pass an config file as an argument" which backs up the ant error of "Problem: failed to create task or type arg".

Java 的错误输出是我的编码错误,意思是“您没有将配置文件作为参数传递”,它支持了“问题:无法创建任务或键入 arg”的 ant 错误。

So how do you pass an argument to an executed JAR file from within Ant? Is this something your not supposed to do?

那么如何从 Ant 内部将参数传递给已执行的 JAR 文件呢?这是你不应该做的事情吗?

回答by Asaph

The <arg>tag should be a child of the <java>tag. Like this:

<arg>标签应该是的子<java>标签。像这样:

<target name="run">
    <java jar="build/jar/ShoutGen.jar" fork="true">
        <arg line="/home/munderwo/workspace/ShoutGen-Java/ShoutGen.conf"/>
    </java>
</target>

In your question <arg>is a sibling of <java>and the argument line is never passed to the javacommand.

在您的问题中<arg>是一个兄弟,<java>并且参数行永远不会传递给java命令。

回答by Ajay

Your arg statement is not properly nested in the java task. It needs to be

您的 arg 语句未正确嵌套在 java 任务中。它需要是

<java jar="...">
  <arg line="..." />
</java>

回答by Ajay

You can do it with something like this, so if no arguments are specified it will continue anyway:

你可以用这样的东西来做,所以如果没有指定参数,它无论如何都会继续:

public static void main(String[] args) {
    try {
        String one = args[0];
        String two = args[1];
    }
    catch (ArrayIndexOutOfBoundsException e){
        System.out.println("ArrayIndexOutOfBoundsException caught");
    }
    finally {

    }
}