Reputation: 1955
Iḿ trying to build an application which collect all tables in my postgres db, by java code.
Process p1 = Runtime.getRuntime().exec("su - postgres -c \"psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \\$1}'\"");
I filtered out some tables in the code (foo, bar) and I only want to collect table names.
This command runs without error in terminal, directly, but doesnt work by my application!
Complete Code:
Process p1 = Runtime.getRuntime().exec("su - postgres -c \"psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \\$1}'\"");
BufferedReader in = new BufferedReader(
new InputStreamReader(p1.getInputStream()));
String line = null;
String result = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
result += line;
}
I've got no errors... But the result string is null
Someone who can help me out?
Thank you in advance
Upvotes: 0
Views: 65
Reputation: 6006
You cannot have double quotes within the exec, as Java does will not interpret it correctly. Try it like so, using this version of Runtime.exec:
String[] myCommand = new String[] {"su", "- postgres", "-c" , "psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \\$1}" };
Process p = Runtime.getRuntime().exec(myCommand);
Upvotes: 1