title: JMeter does not execute Java code correctly if it's ran as .jar file from command line
body: <p>I'm designing JMeter scenario which implies executing a certain .jar file via OS Process Sampler element. My Java code has while loop which basically checks a certain mailbox for a letter with a certain subject. Loop waits until finds one (emails are always delivered with roughly 3 minutes delay), parses it and writes some data to .txt file.</p>
  
  <p>If I run this .jar directly from cmd then the code works as expected. But if I run it via JMeter OS Process Sampler then it never creates a file for me. I do see that email is delivered to inbox, so expect it to be parsed and .txt created.</p>
  
  <p>At first I suspected that JMeter finishes Java scenario without waiting for while loop to execute. Then I put OS Process Sampler in a separate Thread and added a huge delay for this thread in order to wait and make 100% sure that email is delivered and Java only need to parse it but it does not help.</p>
  
  <p>View Results Tree never shows any errors.</p>
  
  <p>Here is my OS Process Sampler: <a href="https://www.screencast.com/t/LomYGShJHAkS" rel="nofollow noreferrer">https://www.screencast.com/t/LomYGShJHAkS</a></p>
  
  <p>This is what I execute via cmd and it works as expected: java -jar mailosaurJavaRun.jar email533.druzey1a@mailosaur.in</p>
  
  <p>And here is my code (it does not looks good but it works):</p>
  
  <pre><code>public class Run {
  
      public static void main(String[] args) {
          MailosaurHelper ms = new MailosaurHelper();
          String arg1 = ms.getFirstLinkInEmail(args[0]);
  
          BufferedWriter output = null;
          try {
              File file = new File("url.txt");
              output = new BufferedWriter(new FileWriter(file));
              output.write(arg1);
          } catch ( IOException e ) {
              e.printStackTrace();
          } finally {
            if ( output != null ) {
              try {
                  output.close();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
            }
          }
      }
  }
  
  public class MailosaurHelper {
  
      protected final String API_KEY = "b3e4d2b193b5eb2";
      protected final String MAILBOX_ID = "d1uzey1a";
  
      public MailboxApi getEmailBox() {
          return new MailboxApi(MAILBOX_ID, API_KEY);
      }
  
      public String getFirstLinkInEmail(String email) {
          MailosaurHelper ms = new MailosaurHelper();
          String link = "";
  
          if (link.equals("") || link.isEmpty()) {
              try {
                  Thread.sleep(3000);
                  link = ms.getAllEmailsByReceipent(email)[0].html.links[0]
                          .toString();
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          return link;
      }
  
      public Email[] getAllEmailsByReceipent(String recepient) {
          try {
              int ifArrayIsEmpty = getEmailBox().getEmailsByRecipient(recepient).length;
              while (ifArrayIsEmpty == 0) {
                  try {
                      Thread.sleep(3000);
                      ifArrayIsEmpty = getEmailBox().getEmailsByRecipient(
                              recepient).length;
                  } catch (InterruptedException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          } catch (MailosaurException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          } catch (IOException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
  
          Email[] listOfEmails = null;
          try {
              listOfEmails = getEmailBox().getEmailsByRecipient(recepient);
          } catch (MailosaurException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          return listOfEmails;
      }
  </code></pre>
  
  <p>The bottom line is that I need to parse Mailosaur email, retrieve URL from it and use it further. Any other suggestion on how to do that using Jmeter/Java/Mailosaur are appreciated.</p>