0

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

1
  • Why don't you connect to Postgres directly and get tables names from the system catalog? This approach looks cumbersome. Commented Jan 14, 2016 at 16:07

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.