I am using this method to send commands to the linux terminal
public static String execute(String command) {
StringBuilder sb = new StringBuilder();
String[] commands = new String[]{"/bin/sh", "-c", command};
try {
Process proc = new ProcessBuilder(commands).start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String s = null;
while ((s = stdInput.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
while ((s = stdError.readLine()) != null) {
sb.append(s);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
It works fine with many commands like "ls", "date" etc..
but I wanna use a psql command to import a sql file to postgre like:
psql -h localhost -p 5432 -U postgres -d test -f test.sql
I typed this command by hand in terminal and it works fine but not with the method above (the method works well with date,ls...)
It looks like the method entries in a kind of infinite loop when it calls psql. The method does not end when the method calls the psql.
ps: I used export PGPASSWORD=pass to avoid pass the password.