-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.

Hexaholic
  • 3,299
  • 7
  • 30
  • 39
Pritam
  • 39
  • 5

1 Answers1

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

Community
  • 1
  • 1
francesco foresti
  • 2,004
  • 20
  • 24