简体   繁体   English

如何配置Ant以从类中运行选择?

[英]How do you configure Ant to get to run selections from the class?

Uhm... srry if there's anything strange with how I word things since it's my first time asking in a site like this. 嗯...很抱歉我的措辞有什么奇怪,因为这是我第一次在这样的网站上提问。 There's still a lot of things that I don't really understand but I'll do my best to try and elaborate my problem. 还有很多我不太了解的事情,但是我会尽力尝试阐明我的问题。

New to Apache Ant, I tried following the steps for running a basic program from the Apache manual , configuring it a bit to try of a different code. 我是Apache Ant的新手,我尝试按照Apache手册中的步骤运行基本程序,并对其进行一些配置以尝试其他代码。 Basically, using ant compile , ant jar , and ant run works out just fine. 基本上,使用ant compileant jarant run可以正常工作。

Following the build.xml setup provided, I tried to do something like a yes-no user input which seemed to turn out okay. 在提供了build.xml设置之后,我尝试执行“是-否”用户输入之类的操作,看起来似乎还可以。

Build.xml Build.xml

<project>

<target name="clean">
    <delete dir="build"/> 
</target>

<target name="compile">          
    <javac srcdir="src" destdir="classes" />  
</target>

<target name="jar">
        <jar destfile="build/jar/Circle.jar" basedir="classes"> 
        <manifest>
            <attribute name="Main-Class" value="Circle"/> 
        </manifest>
    </jar>
</target>

<target name="input"> 
    <input message="Do you want to show the details of a Circle?" validargs="y,n" addproperty="do.delete"/>
    <condition property="do.abort">
        <equals arg1="n" arg2="${do.delete}"/>
    </condition>
    <fail if="do.abort">Build aborted by user.</fail>
</target>

<target name="run" depends="input">
    <java classname="Circle" fork="true"> 
        <classpath path="classes" />
    </java>
</target>

Circle.java Circle.java

import java.lang.*;
public class Circle{
public static void main(String[] args){

    int radius;
    double circumference;
    double areaOfCircle;     
    radius=10;              
    circumference =2* 3.1416*radius;            
    areaOfCircle = 3.1416 * radius *radius;

    System.out.println();
    System.out.println("********************************************************");
    System.out.println("*  Radius of circle is   " + radius +"                 *");
    System.out.println("*  Circumference of circle is   " + circumference +"   *");
    System.out.println("*  Area of circle is   " + areaOfCircle +"             *");
    System.out.println("********************************************************");
} 

} }

How do I configure it so that I can put a selection statement asking for user input in the source code? 如何配置它,以便我可以在源代码中放入选择语句以要求用户输入? Something like removing the input target in the buildfile and putting "Do you want to show the details of a Circle?" 诸如在构建文件中删除输入目标并放入“您想显示圆的细节吗?”之类的东西。 in the Circle file. 在Circle文件中。

From what I understand, the Ant can run classes without the need for something like a main class (?). 据我了解,Ant可以运行类而无需像主类(?)这样的东西。

Incorrect Ant can only run properly compiled Java classes. 错误的Ant只能运行正确编译的Java类。 The "main" method is the standard entry point to a Java program. “ main”方法是Java程序的标准入口点。

So there are two problems with your code. 因此,您的代码有两个问题。

  1. No main method defined in your main class (specified in the Manifest of the jar) 您的主类中没有定义主方法(在jar的清单中指定)
  2. You are invoking the class incorrectly. 您正在错误地调用该类。

Either leverage the fact you have a main class defined 要么利用您定义了主类的事实

<target name="jar">
    <jar destfile="myprog.jar" basedir="classes">
        <manifest>
            <attribute name="Main-Class" value="Circle"/> 
        </manifest>
    </jar>
</target>

<target name="run" depends="jar">
    <java jar="myprog.jar" fork="true"/>
</target>

Or run the code by specifying the classname and classpath. 或通过指定类名和类路径来运行代码。

<target name="run" depends="jar">
    <java classname="Circle" fork="true">
        <classpath>
            <pathelement path="classes"/>
        <classpath>
    </java>
</target>

For a full and very complete example (also covers the advanced topic of managing 3rd party dependencies), see the following: 有关完整且非常完整的示例(还涵盖了管理第三方依赖关系的高级主题),请参见以下内容:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM