-1

I am running a .jar file which produces output in command prompt. I need the output from command prompt window to print it in a UI. I used the following code but it is not working in my case.

package ckmetrics;
import java.io.IOException;

public class CKMetrics {

    public static void main(String[] args) throws IOException {
        // TODO code application logic here
        String command ="java -jar CK-Metrics.jar Test.class";
        String ans = execCmd(command);
        System.out.println(ans);
    }

    public static String execCmd(String cmd) throws java.io.IOException {
        System.out.println(cmd);
        Process proc = Runtime.getRuntime().exec(cmd);
        java.io.InputStream is = proc.getInputStream();
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        String val = "";
        if (s.hasNext()) {
            val = s.next();
            System.out.println("1");
        }
        else {
            val = "";
            System.out.println("2");
        }
        System.out.print(val);
        return val;
    }
}

Every time the the else condition is executed.

0

1 Answer 1

0

Shouldn't

java.io.InputStream is = proc.getInputStream();

be instead

java.io.OutputStream os = proc.getOutputStream();

Also take a look at this : Printing Runtime exec() OutputStream to console

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.