Note that there are some explanatory texts on larger screens.

plurals
  1. POJava's System.getRuntime().exec not behaving as if the shell user called it
    primarykey
    data
    text
    <p>I am running a java application from the console on an HP-UX machine. In it, I generate some reports, zip them, and then email them. Everything is working, except the email.</p> <p>I am using the mail binary to send mail from the command line. Since it's HP-UX, it's a bit different than the standard GNU sendmail.</p> <p>This is the code I'm using to send the mail:</p> <pre><code> public static void EmailReports(String[] recipients, String reportArchive, String subject){ SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy"); String today = dateFormat.format(new Date()); File tempEmailFile; BufferedWriter emailWriter; try { tempEmailFile = File.createTempFile("report_email_" + today, "msg"); emailWriter = new BufferedWriter(new FileWriter(tempEmailFile)); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not create temporary file."); return; } try { emailWriter.write("SUBJECT: " + subject + "\n"); emailWriter.write("FROM: " + FROM + "\n"); emailWriter.write(BODY + "\n"); emailWriter.close(); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not write to temporary file."); } //read the archive in try { FileInputStream archiveIS = new FileInputStream(new File(reportArchive)); OutputStream archiveEncoder = MimeUtility.encode(new FileOutputStream(tempEmailFile, true), "uuencode", Zipper.getArchiveName(reportArchive)); //read archive byte[] buffer = new byte[archiveIS.available()]; //these should never be more than a megabyte or two, so storing it in memory is no big deal. archiveIS.read(buffer); //encode archive archiveEncoder.write(buffer); //close both archiveIS.close(); archiveEncoder.close(); } catch (FileNotFoundException e) { System.out.println("Failed to send email. Could not find archive to email."); e.printStackTrace(); } catch (MessagingException e) { System.out.println("Failed to send email. Could not encode archive."); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not encode archive."); } System.out.println("Sending '" + subject + "' email."); try { Process p = Runtime.getRuntime().exec("mail me@example.com &lt; " + tempEmailFile.getAbsolutePath()); System.out.println("mail me@example.com &lt; " + tempEmailFile.getAbsolutePath()); StringBuffer buffer = new StringBuffer(); while(p.getErrorStream().available() &gt; 0){ buffer.append((char) p.getErrorStream().read()); } System.out.println("STDERR: " + buffer.toString()); buffer = new StringBuffer(); while(p.getInputStream().available() &gt; 0){ buffer.append((char) p.getInputStream().read()); } System.out.println("STDOUT: " + buffer.toString()); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not get access to the shell."); } } </code></pre> <p>When I run the program, and it sends the email, I get a blank email, no subject, no body, no attachment, and it's from the user@hostname from the HP-UX box instead of from the email specified in <code>FROM</code>.</p> <p>However, when I run the same line that it runs (see the command printed out after I call exec), I get the correct email, from the correct user, with a subject, body, and attachment.</p> <p>STDOUT and STDERR are both empty. It's almost as if I'm sending mail a blank file, but when I print the file before I call the exec, it's there.</p> <p>What's going on here?</p> <p>Edit: Attempts made:</p> <p>Using Ksh:</p> <pre><code> try { String cmd = "mail me@example.com.com &lt; " + tempEmailFile.getAbsolutePath(); Runtime.getRuntime().exec(new String[] {"/usr/bin/ksh", cmd}); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not get access to the shell."); } </code></pre> <p>Using STDIN:</p> <pre><code> try { System.out.println("mail me@example.com &lt; " + tempEmailFile.getAbsolutePath()); Process p = Runtime.getRuntime().exec("mail me@example.com "); FileInputStream inFile = new FileInputStream(tempEmailFile); byte[] byteBuffer = new byte[inFile.available()]; inFile.read(byteBuffer); p.getOutputStream().write(byteBuffer); inFile.close(); p.getOutputStream().close(); StringBuffer buffer = new StringBuffer(); while(p.getErrorStream().available() &gt; 0){ buffer.append((char) p.getErrorStream().read()); } System.out.println("STDERR: " + buffer.toString()); buffer = new StringBuffer(); while(p.getInputStream().available() &gt; 0){ buffer.append((char) p.getInputStream().read()); } System.out.println("STDOUT: " + buffer.toString()); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to send email. Could not get access to the shell."); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload