We have a third-party Java application that uses System.console().readPassword() method. It cannot be modified. We need to call it from a non-interactive shell script that should pass password to that application. Piping password to the application does not appear to work. Is there a way to do this?
Test program:
import java.io.Console; public class Main { public static void main(String[] args) throws Exception { Console console = System.console(); if (console == null) { System.err.println("Cannot acquire console"); System.exit(-1); } char[] passwordArray = console.readPassword("Enter password: "); System.out.println("Password: " + String.valueOf(passwordArray)); } } Test shell script that does not work:
#!/bin/sh export TEST_PASSWORD="test password" echo "Test Password: $TEST_PASSWORD" echo $TEST_PASSWORD | java Main
expectorscriptscript "echo xx | java Main"still results in "Cannot acquire console". Voting to reopen so that someone can add a specific answer (the dupe is not directly relevant).scriptdoesn't work, tryexpect. You can find examples here: phoenixnap.com/kb/linux-expect. First,spawnyour Java app,expectthe "Enter password:" prompt, thensendyour password.expectseems to do what I need.