1

I can run the following via bash and it will work:

psql -h <host> -p <port> -d <dbname> -U <username> -w -v username="'user'" -v rid=2 -c '\ir ../../../resources/sql/myFile.sql'

When I try to run the command in Java using the following, it doesn't do anything but it doesn't throw an error either. Is there a way I can log what the exec in Java is doing so I can see why it isn't working?

try {
        String line;
        log.error("Working Directory = " +
                System.getProperty("user.dir"));

        Process p = Runtime.getRuntime().exec("psql -h <host> -p <port> -d <dbname> -U <username> -w -v username=\"'user'\" -v " +
                "rid=2 -c '\\ir sql/myFile.sql'");


        log.error("HERE");
        BufferedReader input =new BufferedReader(new InputStreamReader(p.getInputStream()));

        while ((line = input.readLine()) != null) {
            log.error(line);
        }
        input.close();
        log.error("here2");
    } catch (Exception e) {
        log.error("Error occurred" + e.toString());
    }

HERE and here2 print out to the console, but nothing in Error occurred.

The process exit value is 1.

reutsey
  • 1,743
  • 1
  • 17
  • 36

1 Answers1

1

In case other people run into this as well. I was able to get more information about errors by using the following:

        InputStream stderr = p.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = p.waitFor();
        System.out.println("Process exitValue: " + exitVal);

I found this in this article: http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

reutsey
  • 1,743
  • 1
  • 17
  • 36