Search code examples
javaprocesssendmailprocessbuilder

Java SendMail sends mail after server is stopped


I have a UI wrapper for a jar. When the event 'submitButton' is triggered from the UI, the following method is called to execute it. The jar executes in a separate process, but the mail is sent only after the calling process (i.e. UI server) is stopped. Any idea why ? (The jar works fine when executed from cmd line).

public static String doSendMail(){
        // Run a java app in a separate system process
        Process process;
        try {
            ProcessBuilder pb = new ProcessBuilder("java",  "-jar", "SendMail.jar",  ">>", "test.log");
            process = pb.start();
            InputStream in = process.getInputStream();
            InputStream err = process.getErrorStream();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "Success";

    }

Solution

  • A couple of problems: >> is shell redirection. You are running the subprocess directly, so it probably got ignored. Also, you never handled the output written from the process, so it likely got buffered and blocked the process until the UI ended.