0

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. Basically, using ant compile, ant jar, and ant run works out just fine.

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

<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

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.

noowt
  • 1
  • 2

1 Answers1

0

From what I understand, the Ant can run classes without the need for something like a main class (?).

Incorrect Ant can only run properly compiled Java classes. The "main" method is the standard entry point to a Java program.

So there are two problems with your code.

  1. No main method defined in your main class (specified in the Manifest of the 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:

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Ah, thanks for the clarification on Ant only running compiled classes. I added a main to the Circle and added a destfile in jar. Should compile properly now. – noowt Sep 07 '16 at 22:35