Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8140493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:11:02+00:00 2026-06-06T12:11:02+00:00

I am running a java application from the console on an HP-UX machine. In

  • 0

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.

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.

This is the code I’m using to send the mail:

    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 < " + tempEmailFile.getAbsolutePath());
            System.out.println("mail me@example.com < " + tempEmailFile.getAbsolutePath());

            StringBuffer buffer = new StringBuffer();
            while(p.getErrorStream().available() > 0){
                buffer.append((char) p.getErrorStream().read());
            }

            System.out.println("STDERR: " + buffer.toString());

            buffer = new StringBuffer();
            while(p.getInputStream().available() > 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.");
        }
    }

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 FROM.

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.

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.

What’s going on here?

Edit: Attempts made:

Using Ksh:

    try {
        String cmd = "mail me@example.com.com < " + 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.");
    }

Using STDIN:

    try {
        System.out.println("mail me@example.com < " + 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() > 0){
            buffer.append((char) p.getErrorStream().read());
        }

        System.out.println("STDERR: " + buffer.toString());

        buffer = new StringBuffer();
        while(p.getInputStream().available() > 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.");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:11:04+00:00Added an answer on June 6, 2026 at 12:11 pm

    I strongly suspect the problem is the redirection. That’s normally handled by the shell – and there’s no shell here.

    Either you need to execute the process normally and then get the process’s standard input stream and write to it from Java, or (probably simpler) run /bin/sh (or whatever) to get the shell to do the redirection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Swing application (currently running on Java 1.6 update 11) which
I have java application which I am running on Unix from the command prompt.
All, I am having some trouble running a java web start application that has
I'm running a Java application from Eclipse that need a lot of memory. Where
I want to move a long-running process in an Java EE application from synchronous
I am working with a large Java web application from a commercial vendor. I've
I have a java application running as a windows service using procrun from Commons
We have a Java application running on JBoss 5.1 and in some cases we
From a java application running on an EC2 instance, I'd like to know what
I have an Java application running in JBoss in which I have enabled JMX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.