title: Pass a string with spaces to an execute command in Java to execute a bash script
body: <p>How do I pass a string with spaces to an execute command in Java to execute a bash script?</p>
  
  <p>I'm trying to use a script to generate and send a email using the unix <code>mail</code> command but it ignores the quotes surrounding the message string:</p>
  
  <pre><code>#!/bin/bash
  
  ######################################################
  #
  # Params:
  # 1) Email recipient
  # 2) Subject line (in quotes)
  # 3) Message (in quotes)
  #
  # Returns:
  # 0 if success, else non-zero
  #####################################################
  
  MAIL_TO=$1
  SUBJECT_LINE=$2
  MESSAGE=$3
  
  echo "Mail To= ${MAIL_TO}" &gt;&gt; /logs/terminalLog.txt
  echo "Subject= ${SUBJECT_LINE}" &gt;&gt; /logs/terminalLog.txt
  echo "Message= ${MESSAGE}" &gt;&gt; /logs/terminalLog.txt
  echo "" &gt;&gt; /logs/terminalLog.txt
  
  echo "$MESSAGE" | mail -s "$SUBJECT_LINE" $MAIL_TO &gt;&gt; /logs/terminalLog.txt
  </code></pre>
  
  <p>and this is how I'm calling it in  java :</p>
  
  <pre><code>Process proc = Runtime.getRuntime().exec(scriptName+" me@someplace.com \"My Test Subject Line\" \"This is the test message!!\"");
  </code></pre>
  
  <p>The problem is it takes <code>"My</code> as the subject argument and <code>Test</code> as the message argument and ignores the rest. </p>
  
  <p>I have tried using single quotes, <code>exec(command, args)</code> where </p>
  
  <pre><code>args = {"me@someplace.com","My Test Subject Line","This is the test message!!" } 
  </code></pre>
  
  <p>but still has the same result.</p>
  
  <p>I have searched here and online but most people seem to suggests what I have tried and while it worked for them, it did not for me.</p>
  
  <h2>Update</h2>
  
  <p>On the Advice of a number of comments/Answers I have changed from using the <code>exec</code> to</p>
  
  <pre><code>ProcessBuilder pb = new ProcessBuilder(scriptName, "me@somePlace.com","My Test Subject Line", "This is the test message!!");
  pb.start();
  </code></pre>
  
  <p>I now successfull get the correct Arguments as shown by the output to log BUT I never recieve the email. If I call the script via the terminal manually I do recieve the email</p>