0

I have a situation where I want (need) do send a file by e-mail at the execution of a given java program. The copy commands are executed fine either on Windows and Linux, however, mutt is not working when the execution is ordered on java. The command string is generated flawless, (I currently manually type the string printed to console to manually send the e-mail), however the Runtime.getRuntime.exec() is not executing it properly.

        if (OS.indexOf("win") >= 0)
        {
            // windows
            cmd = "cmd /C copy "+workbookFileName+" "+latestWorkbookFileName;
            System.out.println("executing command windows: "+cmd);
            Runtime.getRuntime().exec(cmd);
        }
        else
        {
   //         others
            cmd = "cp "+workbookFileName+" "+latestWorkbookFileName;
            System.out.println("executing Linux: "+cmd);
            Runtime.getRuntime().exec(cmd);



            cmd = "echo \"This is the message body\" | mutt -a \""+workbookFileName+"\" -s \"message subject\" -- sqlinjection@domain.com";
            System.out.println("executing Linux: "+cmd);
            Runtime.getRuntime().exec(cmd);

            System.out.println("bye");


        }
SQL.injection
  • 2,607
  • 5
  • 20
  • 37

2 Answers2

0

This is a good tutorial on how to use Runtime.exec When Runtime.exec() won't.

The idea is that you need to consume the standard input, output and error for the process created by Runtime.exec and the site i mentioned is perfect.

Claudiu
  • 1,469
  • 13
  • 21
  • I used the StreamGobbler, the process exits with value 0 (success), the standard input is consumed, but i don't get the e-mail on my inbox :( – SQL.injection Sep 09 '13 at 10:28
  • try replacing `cmd = "echo \"This is the message body\" | mutt -a \""+workbookFileName+"\" -s \"message subject\" -- sqlinjection@domain.com";` with `cmd = "bash -ic 'echo \"This is the message body\" | mutt -a \""+workbookFileName+"\" -s \"message subject\" -- sqlinjection@domain.com' ";` basically run the whole command inside a shell. I had the same problem once. – Claudiu Sep 09 '13 at 10:30
0

You need to:

  1. consume stdout/stderr from the spawned process. Note that you'll have to do this in separate threads in order to avoid blocking the process whilst it waits for you to read. See this answer for more details
  2. use Process.waitFor() to collect the exit code of the spawned process

Unrelated: Check out Files.copy() to perform cross-platform file copy operations

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440