java 添加一个 ant 目标以从 jar 文件中运行一个类

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

Add an ant target to run a class from within a jar file

javaant

提问by blue-sky

I have a jar file with multiple executable classes, how can I run the main method of a using an ant target ?

我有一个包含多个可执行类的 jar 文件,如何使用 ant 目标运行 a 的主要方法?

Thanks

谢谢

回答by dogbane

Take a look at the Ant Java Task. You should be able to create a target that looks like this:

查看Ant Java 任务。您应该能够创建一个如下所示的目标:

<target name="mytarget" description="runs my class" >
  <java classname="test.Main">
    <classpath>
      <pathelement location="dist/test.jar"/>
    </classpath>
  </java>
</target>

Alternative, using Ant Exec Task:

替代方案,使用Ant Exec Task

<target name="mytarget" description="runs my class">
     <exec executable="java">
       <arg line="-classpath dist/test.jar test.Main"/>
     </exec>
</target>

回答by khachik

Using ant's java task:

使用 ant 的 java 任务:

<java fork="yes" classname="com.example.Class" failonerror="true">
  <classpath>
    <pathelement path="path/to/jar/containing/the/com.example.Class"/>
    ...
  </classpath>
  ...
</java>

回答by AlexR

First you have to decide which class is used as entry point.

首先,您必须决定使用哪个类作为入口点。

Let's assume that the class is com.mycompany.Main

让我们假设这个类是 com.mycompany.Main

in this case if you wish to run application from command line say

在这种情况下,如果您希望从命令行运行应用程序,请说

java -cp my.jar com.mycompany.Main

Now you can either run it as java program:

现在您可以将它作为 java 程序运行:

   <java classname="com.mycompany.Main">
     <classpath>
       <pathelement location="myjar.jar"/>
     </classpath>
   </java>

(see http://ant.apache.org/manual/Tasks/java.html)

(见http://ant.apache.org/manual/Tasks/java.html

or run it as an generic external process:

或将其作为通用外部进程运行:

(see http://ant.apache.org/manual/Tasks/exec.html).

(参见http://ant.apache.org/manual/Tasks/exec.html)。

I think that using java target is preferable.

我认为使用 java 目标是可取的。