Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically answer user prompts in Java?

I'm using a compiled Java file which takes a filename as a parameter and then asks for a username and password (via user input). I am trying to automate this from a shell script, but am running into problems. I'm unable to access the java code, which is why I am having trouble.
So the code is called the following way

java javaprogram /home/user/securityfile

So, you pass the file, and then it asks you for username and password. After entering those, it's done.

Now, I have tried to put the user input into a file and pass it in, but I get an error.

java javaprogram /home/user/securityfile < userinputfile

userinput file contains the following text (First line is the username, second line is the password):

MattSmith
MSpassword136

Does anyone have any ideas? Maybe I am doing it wrong?

Here is the error message:

Exception in thread "main" java.lang.NullPointerException at ilex.util.UserAuthFile.main(UserAuthFile.java:297) 

Thanks

like image 504
jonesy19 Avatar asked May 20 '26 02:05

jonesy19


1 Answers

Try (echo "user"; sleep 1; echo "password") | java javaprogram /home/user/securityfile

If that works you can refactor to read from file without too much work. If it doesn't, may be that app will never read from System.in since it requires an interactive console System.console(). Some info here.

like image 81
LMC Avatar answered May 21 '26 14:05

LMC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!