2

We have python application for work with WebSphere queue. This application consists of two files (queue-tools, queue-tools.py) and few libs. queue-tools file is looking like this:

#!/bin/sh

$HOME/sh/tests/queue-tool.py $@

When I run queue-tool -h command on unix host I see next result:

queue-tool.py [-h] [-t host] [-q qmgr] [-c channel] [-s SecurityExit] [-w]
                 [-f [FILE]] [-put arg [arg ...]] [-get arg [arg ...]]
                 [-peek arg [arg ...]] [-inquire arg]

Queue Tool

optional arguments:
  -h, --help           show this help message and exit
  -t host              target host
  -q qmgr              queue manager
  -c channel           channel

So my question is how to run this command (queue-tools -h) from java code and read output result? I use below code but it doesn't work:

public static void testScript() {
    String line;
    try {
        String[] cmd = {"/bin/sh", "-c", "queue-tool -h"};
        Process p = Runtime.getRuntime().exec(cmd);
        p.waitFor();
        BufferedReader in =
                new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = in.readLine()) != null) { //line is null so nothing to read
            System.out.println(line);
        }
        in.close();
    } catch (InterruptedException iEX) {
        iEX.printStackTrace();
    } catch (IOException ioEX) {
        ioEX.printStackTrace();
    }
}
Iurii
  • 1,755
  • 8
  • 24
  • 38
  • Get rid of the p.waitFor(). If I am not mistaken, it makes it finish the process and then return to your thread, after which no output would be captured. – MiltoxBeyond Jun 15 '15 at 16:12
  • Here is the solution: http://stackoverflow.com/questions/3774432/starting-a-process-in-java – MiltoxBeyond Jun 15 '15 at 16:15
  • @MiltoxBeyond p.waitFor() return 127 value. – Iurii Jun 15 '15 at 16:19
  • @MiltoxBeyond could you please add answer on this question, just add above link and I update this question as answered. – Iurii Jun 15 '15 at 16:35
  • @Paul I just put full path to queue-tool script, like this `{ "/bin/sh", "-c", "/apps/scripts/sh/tests/queue-tool -h"}` – Iurii Jun 15 '15 at 16:51

1 Answers1

0

Get rid of the p.waitFor(). If I am not mistaken, it makes it finish the process and then return to your thread, after which no output would be captured.

Just as reference I also found the solution at: Starting a process in Java?

Community
  • 1
  • 1
MiltoxBeyond
  • 2,683
  • 1
  • 13
  • 12