6

I know this question has been asked before but those answers didn't provide me an answer.

I want to execute a exec jar file in my java program and get the output from executing jar into a string. Here below are the codes I have used so far without success.

cmdlink = "java -jar iwtest-mac.jar"+" "+cmd;
            System.out.println(cmdlink);
             Process process = Runtime.getRuntime().exec(cmdlink);
             BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
             while ((reader.readLine()) != null) {
                 st = reader.readLine();  

             }
             process.waitFor();

and another code I have tried is as follows:

String cmdlink = "iwtest-mac.jar "+cmd;    
          ProcessBuilder pb = new ProcessBuilder("java", "-jar", cmdlink); //cmd here is a string that contains inline arguments for jar.
            pb.redirectErrorStream(true);
            pb.directory(new File("C:\\Users\\Dharma"));

            System.out.println("Directory: " + pb.directory().getAbsolutePath());
            Process p = pb.start();
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                    System.out.println( line ); 
            p.waitFor();

Both of the above are not working for me. Any suggestions are appreciated.

1 Answer 1

14

This works For Me..

public class JarRunner {
    public static void main(String[] args) {
        ProcessBuilder pb = new ProcessBuilder("java", "-jar", "C:\\JCcc.jar");
        pb.directory(new File("C:\\"));
        try {
            Process p = pb.start();
            LogStreamReader lsr = new LogStreamReader(p.getInputStream());
            Thread thread = new Thread(lsr, "LogStreamReader");
            thread.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class LogStreamReader implements Runnable {

    private BufferedReader reader;

    public LogStreamReader(InputStream is) {
        this.reader = new BufferedReader(new InputStreamReader(is));
    }

    public void run() {
        try {
            String line = reader.readLine();
            while (line != null) {
                System.out.println(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

This is what the Docs says-

ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");

You can pass any number of arguments in constructor.

Read more about process builder here.

Sign up to request clarification or add additional context in comments.

2 Comments

excuse me if i need to give command line arguments for my jar where should i specify them in the above code?
Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: -jar i got the above errors

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.