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?

3
  • 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? Commented Oct 7, 2015 at 21:38
  • Yes that was typo. exit value i am getting is exit: 1 Commented Oct 7, 2015 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. Commented Oct 7, 2015 at 21:49

2 Answers 2

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

1 Comment

Thanks. That fix that.
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.

1 Comment

Well, that wouldn't compile, OP says it doesn't return output. Perhaps it's just a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.