title: how to run a java executable jar in another java program
body: <p>I know this question has been asked before but those answers didn't provide me an answer.</p>
  
  <p>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.</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>and another code I have tried is as follows:</p>
  
  <pre><code>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();
  </code></pre>
  
  <p>Both of the above are not working for me. Any suggestions are appreciated.</p>