• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime().exec() doubt

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do we need to call

p.destroy

once the execution is complete/exception occurs???

or nullify the process handlers...??
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic