File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Runtime.getRuntime().exec() doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime.getRuntime().exec() doubt" Watch "Runtime.getRuntime().exec() doubt" New topic
Author

Runtime.getRuntime().exec() doubt

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

From a java program,I want to send a mail...using the sendmail program of the linux machine

This is the command

/usr/sbin/sendmail kumar@Kumar.com <Mail.txt


And the code snippet is



The command is getting executed without any exceptions...but the Mail.txt contents are not coming through in the mail..

What have i missed??

Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

You can't use piping (|) or input/output/error redirection in the exec methods.

Instead, you need to do this yourself. In this case, just copy all input from the file to the process:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks but is





Process p = Runtime.getRuntime().exec("/usr/sbin/sendmail kumar@Kumar.com");

OutputStream code...sufficient to send out the mail message...

(ignoring the address part)

and when the stream is closed ..message is eent out........

Anything else ...??
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Do we need to call

p.destroy

once the execution is complete/exception occurs???

or nullify the process handlers...??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19726
    
  20

destroy() should be called to kill the process while it is still running. Calling it when the process has already ended, either successfully or not, does not make any sense anymore since there is no more process to kill.

When the process ends, all is cleaned up automatically. Nullifying never hurts, but it's not necessary.
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Thanks Rob for clearing the doubt..and i am able to send the mail by handling the

But whats the way if one wants to send an attachment to the mail...


Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.getRuntime().exec() doubt