Calling the native Linux mutt module from Java...

K

K2

First, please don't flame me (I love folks who start posts like this) if
you feel this is not completely on topic, as it is related.

I'm attempting to perform a command-line operation from the Linux based
mutt module which simply sends an email with attachment via a call with
Runtime.getRuntime().exec()
The command as formatted performs flawlessly when run directly within
the Linux shell environment, but fails when run from the Java process.
The only thing I've determined is that the return code is "1" which I
assume simply means "error" (I've been flamed by the mutt group who had
this remarkable insight for me).

Anyone here used this or a similar module with success or have some
recommendations as to how to debug this?
 
G

Gordon Beaton

I'm attempting to perform a command-line operation from the Linux
based mutt module which simply sends an email with attachment via a
call with Runtime.getRuntime().exec()

The command as formatted performs flawlessly when run directly
within the Linux shell environment, but fails when run from the Java
process. The only thing I've determined is that the return code is
"1" which I assume simply means "error" (I've been flamed by the
mutt group who had this remarkable insight for me).

Anyone here used this or a similar module with success or have some
recommendations as to how to debug this?

Read from the child process' stdout and stderr streams, the program
probably tells you why it fails. You should do that anyway, since
failing to do so could cause it to hang.

Other than that, post the relevant code and the exact command line
that fails.

/gordon
 
K

K2

Gordon said:
Read from the child process' stdout and stderr streams, the program
probably tells you why it fails. You should do that anyway, since
failing to do so could cause it to hang.

Other than that, post the relevant code and the exact command line
that fails.

Thanks Gordon, that was helpful, however it brought up another issue.
I'm providing the demo code here that includes the comments on setting
it up and the problem that I've run into...

-------------- <MuttProblem.java> --------------------------------
import java.io.*;

public class MuttProblem {

/**
* Entry point to execute the MuttProblem process.
*
* For the sake of this demo will need to create the text file
* /tmp/results.txt prior to running. This will be the message
* text that will be included in the body of the resulting email.
*
* @param args Passed from the calling process. For purposes of
* this example it requires a valid email address to post
* the resulting email to.
*/
public static void main(String[] args) {
if(args.length == 0)
throw new RuntimeException("You must provide the target" +
" email address as the first arguement\n" +
"for purposes of this example");
try{
// if location of mutt is different then the location
// stated here adjust accordingly...
String []testArgs = { "/usr/bin/mutt",
"-s \"Unit Tests results\"",
"-i/tmp/results.txt", "-c" + args[0]};

// I've also attempted simply passing in the testCommand
//String here. Same result.
Process muttProcess = Runtime.getRuntime().exec(testArgs);

/* If this is uncommented and the exec should succeed, the call to
waitFor() hangs. While this helped me debug some of the problems I
had in working out the arguement format issues I had, it doesn't
do me much good in production enviroment. Any ideas on why it hangs?
Is this caused by the mutt process or ?

muttProcess.waitFor();
System.out.println("Exit value = " +
muttProcess.exitValue());
if( muttProcess.exitValue() != 0 ){
BufferedReader brr =
new BufferedReader(new InputStreamReader
( (muttProcess.getErrorStream())));
String line;
while(null != (line = brr.readLine())) {
System.out.println(line);
}
}
*/
} catch (Exception ex) {
ex.printStackTrace(System.out);
}
}
}
-------------- </MuttProblem.java> -------------------------------

thanks again for any information.
 
G

Gordon Beaton

Thanks Gordon, that was helpful, however it brought up another
issue. I'm providing the demo code here that includes the comments
on setting it up and the problem that I've run into...

A couple of things:

- read from both output and error streams *while* the process is
runnning, not afterwards. There is limited buffer space available
for the process write to until you start reading, so not reading
while the process runs could cause it to block if it has more output
than will fit.

- when you tokenize the command line as you've done, put each separate
argument in its own String. As written, the first argument to mutt
is "-s \"Unit Tests results\"", which I suspect should be two
separate arguments, the option and its value. Note that there is no
need to add extra quotation marks around arguments with spaces; the
command line won't be processed or tokenized any further. Write it
like this instead:

String[] testArgs = {
"/usr/bin/mutt",
"-s",
"Unit Tests results",
"-i",
"/tmp/results.txt",
"-c",
args[0]
};

- close all three Process streams after the child process has
finished.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top