0

I am running command using Java and getting no output.

Process p;
Runtime run = Runtime.getRuntime();  
    String s1 = "queryData 1005017 --format '\"%s" scope'";

    System.out.println("Command is " + s1);


    try {  

        p = run.exec(s1);  
        BufferedReader br = new BufferedReader(
                new InputStreamReader(p.getInputStream()));
            while ((s = br.readLine()) != null)
                System.out.println("line: " + s);
        p.getErrorStream();  
        p.waitFor();

    }  

While the same command ---> queryData 1005017 --format '"%s" scope' runs without any issue. Wondering am i missing any thing while handling either double quote, or % sign?

Ammad
  • 4,031
  • 12
  • 39
  • 62
  • 1
    Assuming that your missing backslash is just a typo (does your program compile?), have you tried checking `p.exitValue()` after the `waitFor` to see if it terminated successfully or not? – RealSkeptic Oct 07 '15 at 21:38
  • Yes that was typo. exit value i am getting is exit: 1 – Ammad Oct 07 '15 at 21:47
  • So, it exited unsuccessfully. Try reading the error stream instead of the input stream and see what kind of error it gives you. – RealSkeptic Oct 07 '15 at 21:49

2 Answers2

2

You didn't escape the internal quotes properly:

String s1 = "queryData 1005017 --format '\"%s" scope'";
            ^--start java string             ^--end java string
                                               ^^^^^ what's this mean to java?

You probably want

String s1 = "queryData 1005017 --format '\"%s\" scope'";
                                             ^--note this

instead.

Marc B
  • 356,200
  • 43
  • 426
  • 500
2

Try do NOT use strings to start processes from Java. Correct way is usage of ProcessBuilder:

p = new ProcessBuilder(Arrays.asList(
    "queryData", "1005017", "--format", "\"%s\" scope")).start();
ursa
  • 4,404
  • 1
  • 24
  • 38