Reputation: 553
I have a jar file which asks user the value of n
. And adds the values entered. When the jar is executed from cmd.exe
, works well. But when invoked from .bat
file, it is not prompting for the input rather executes the further statements. I tried using pipe,as,
(echo 3
echo 10
echo 20
echo 30)| java -jar add.jar
but didn't work.How can I automate the input?
Note: values are not accepted as arguments, but as a prompt.
Upvotes: 1
Views: 910
Reputation: 22963
Without knowing something about the code it's hard to tell why it's not working for you.
See below a simple working example
Add.java
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int sum = 0;
while (scanner.hasNextInt()) {
int value = scanner.nextInt();
sum += value;
System.out.println("sum = " + sum);
}
}
}
run.bat
@echo off
(echo 2
echo 10
echo 20
echo 30
echo end ) | java -jar Add.jar
compile and build the jar
javac Add.java
echo Main-Class: Add > manifest.mf
jar cmf manifest.mf Add.jar Add.class
run the batch file
run.bat
output
sum = 2
sum = 12
sum = 32
sum = 62
Upvotes: 2