Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

java program to accept user input, ant

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

My java program looks like below:

main method{
System.out.println("Enter 1 to add X.");
System.out.println("Enter 2 to add Y");
System.out.println("Enter 3 to add Z.");

and upon selection of above option, I ask user to input more information
regarding their previous selection and then processing starts.

This program works just fine as standalone application but when tried to run through ant it just hangs at the first user input prompt
[eclipse is my IDE]

Any help is greatly appreciated.
Thanks,
Kriss
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have the "input" attribute set for the "java" task in Ant? If you do, remove it - without that attribute the app should get input from the console.

If this does not help, post the full source code for the app and the source for the Ant task used to run the app. Don't forget to use 'code' UBBCode tags!
 
Kriss Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Peter,
Thanks for your response. I do not have input attribute set for the java task. My java program expects user input from the console. My ant task is as below:
<target name="generate" description="Execute the java class file">
<java classname="com.config.utilities.ConfigUtilityTool"
classpathref="run.classpath" fork="true"></java>
</target>

Tried with both options for fork attribute but no success.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello,

My java program looks like below:

main method{
System.out.println("Enter 1 to add X.");
System.out.println("Enter 2 to add Y");
System.out.println("Enter 3 to add Z.");

and upon selection of above option, I ask user to input more information
regarding their previous selection and then processing starts.

This program works just fine as standalone application but when tried to run through ant it just hangs at the first user input prompt
[eclipse is my IDE]

Any help is greatly appreciated.
Thanks,
Kriss



Hello Kriss,

There is a workaround here, make a batch file with the following code:-


But make sure, the java class 'ConfigUtilityTool' has been compiled before calling this batch file.

For calling this batch file use Exec Task

Let me know if it works fine.
[ November 08, 2008: Message edited by: Manish Doomra ]
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try setting "fork" to false. By setting it to true, the app runs in its own VM and does not have access to the stdin used by Ant.
 
Kriss Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Johnson:
Try setting "fork" to false. By setting it to true, the app runs in its own VM and does not have access to the stdin used by Ant.



Hello Peter,
Thanks for your help. Behaviour did not change by setting fork to false.
 
Kriss Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manish Doomra:


Hello Kriss,

There is a workaround here, make a batch file with the following code:-


But make sure, the java class 'ConfigUtilityTool' has been compiled before calling this batch file.

For calling this batch file use Exec Task

Let me know if it works fine.

[ November 08, 2008: Message edited by: Manish Doomra ]



Hello Manish,
Thanks for your help. Your proposed workaround is not waiting for user to input, atleast the way I implemented it. Let me share below what I did so far:-

My code file:



My ant script:
<target name="testget">
<exec executable="cmd">
<arg value="/c"/>
<arg value="util.bat"/>
</exec>
</target>

and my .bat file:
@echo off
java -classpath . com.test.ant.TestAnt
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Ant task should also work (I used you same code, it worked for me - I compiled into the target/classes directory, you might have to adjust the classpath)



results:



Note that I entered the input 'peter' before the prompt was shown. If you want the prompt to show up before you enter the name, change this one line to use println (instead of print):

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that your problem may be that the program is not hanging, just that it didn't print the prompts and is waiting for input from the console.

Try entering the data that the prompts would expect and see what happens. When I run a program that asks for about 20 values, I have to enter them all with no prompts, and then when I am done I get a flood of all 20 prompts in a row followed by the rest of the output written after that. It seems that Ant buffers everything the java program prints and dumps it to the console when the java task is done.
 
Peter Johnson
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, welcome to Java Ranch!

Note this sentence in my post:

If you want the prompt to show up before you enter the name, change this one line to use println (instead of print):

 
Kriss Reddy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help. The problem was with Eclipse IDE not with my java program or ant script.
 
Yes, my master! Here is the tiny ad you asked for:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic