1 2 Previous Next 22 Replies Latest reply on Sep 8, 2004 9:07 PM by 807541 Go to original post
      • 15. Re: getRuntime().exec
        807541
        It's a pretty good bet that you are bumping into some weird kind of error. You should grab the standard and error output from the process. Something like this:
        Process child = Runtime.getRuntime().exec("/bin/sh -c /bin/echo 'Body Text' | /bin/mail -s 'Subject' user@domain.com");
        
        // Get the standard output of the child process
        BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
        // Get the error output of the child process
        BufferedReader err = new BufferedReader(new InputStreamReader(child.getErrorStream()));
        
        System.out.println("Standard Output:");
        while(in.ready()) {
            System.out.println(in.readLine());
        }
        
        System.out.println("\nError Output:");
        while(err.ready()) {
            System.out.println(err.readLine());
        }
        • 16. Re: getRuntime().exec
          807541
          Seriously... When I pass the following there is no standard output or error output.

          String[] cmd = {"/bin/sh","-c","/bin/echo 'Body Text' | /bin/mail -s 'Subject' nathan@domain.com"};

          Nathan
          • 17. Re: getRuntime().exec
            807541
            Somebody smack me. It works when I pass the previous code exec. ????????

            Nathan
            • 18. Re: getRuntime().exec
              807541
              /me smack's NathanAaron

              feel better?
              • 19. Re: getRuntime().exec
                807541
                hangs head in shame
                • 20. Re: getRuntime().exec
                  807541
                  What do you mean - using a single line? I still can't get that to work!
                  • 21. Re: getRuntime().exec
                    807541
                    Hey man, dont let it get ya down. I can't count the number of times when I had something coded up and would swear up and down "IT SHOULD WORK!" but for some reason it didn't.
                    • 22. Re: getRuntime().exec
                      807541
                      Here is a brief snippet of my code. I still have some cleanup to do but it works. Not sure what I was doing wrong before.

                      Method I call to send the mail:

                      // send email
                      public static void sendMail(String configfile, String message) {
                      // get values from the properties file using the getProperty method
                      String sh = getProperty(configfile, "sh");
                      String echo = getProperty(configfile, "echo");
                      String mail = getProperty(configfile, "mail");
                      String subject = getProperty(configfile, "subject");
                      String address = getProperty(configfile, "address");

                      try {
                      // commands to execute
                      String[] cmd = {sh,"-c", echo + " \'" + message + "\' | " + mail + " -s \'" + subject + "\' " + address};
                      // execute commands
                      Process child = Runtime.getRuntime().exec(cmd);
                      // get result code
                      int exitVal = child.waitFor();
                      } catch (Exception e) {
                      System.out.println("Cannot execute " + mail + ".");
                      } finally {
                      }
                      }

                      My properties file:

                      sh = /bin/sh
                      echo = /bin/echo

                      mail = /bin/mail
                      subject = Email from CMB
                      address = user@domain.com

                      1 2 Previous Next