Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.in input, java but with apache ant

I have added few system.in inputs (user inputs) in an interactive way. I have created executable jar and I am using apache ant to compile and run the program. When I execute it with java -jar jarfile.jar, the program interacts fine and take the user input through buffered reader system.in, but when I run it through apache ant by ant run, it hangs after taking first input.

Why with apache ant, it is not taking System.in inputs typed through keyboard?

Do I have to add something in the run target java task of apache ant?

like image 395
Natha Kamat Avatar asked Jun 28 '26 00:06

Natha Kamat


1 Answers

Reading console input from the task is not allowed.

But you can do take console inputs from the user with ant and pass it to the command line argument to the Java program.

Following is the sample ant script which is taking input from the user and passing it to the java program. And Java program is printing it.

Ant Script:

<project name="Testing" basedir="../bin" default="run">
  <target name ="run">
    <property name="name" value="Test"/>
    <input message="Enter your Name :" addproperty="inputvalue"  defaultvalue="n" />
    <echo message="${inputvalue}"/>
        <java classname="${name}" failonerror="true" dir="${basedir}" spawn="false" fork="false"  >
        <classpath>
            <pathelement location="${basedir}" />
        </classpath>
        <arg value="${inputvalue}"/>
    </java>
  </target>
</project>

Java Program:

public class Test {

public static void main(String[] args) throws IOException {

        System.out.println("Hello " + args[0]);  
}
}
like image 160
Niraj Chapla Avatar answered Jul 01 '26 22:07

Niraj Chapla