3

I wanted to know if there is a way, from a sh script, to input value in a Java program. For example I have a program.jar. I do java -jar program.jar and it outputs:

Enter your name: 

Would it be possible to write an sh script like that:

java -jar program
echo name

where name is gonna be the input for the program?

4 Answers 4

5

To echo data into your java program from the shell do something like this:

java -jar program<< EOF
<your-data>
EOF
Sign up to request clarification or add additional context in comments.

Comments

2

In addition to @tcb 's suggestion, you can also use a standard input redirection pipe < in order to specify a file that will be the input of the program:

input.txt

name

And in the sh:

java -jar program < input.txt

Comments

1

Use pipes:

echo name | java -jar program

1 Comment

Isn't that cat name? Surely if the input was literally "name" then the OP would have already understood to write java -jar program name.
1

If you mean interactive input , expect is always one of the answers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.