• Post Reply Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime.exec()  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a unix command and i am executing it from java using Runtime.getRuntime.exec() method. The unix command basically triggers email.
The command is
/nova/bin/sendfile -f <from address> -t <to address> -c <cc address> -s <mail subject>.
When i execute this command from java using Runtime.getRuntime.exec(), i am getting the email for say 2 recipients. if i increase the recipients to more than 2, then the email is not triggered. i neither get an error nor an exception. the command is executed successfully but i am not getting the email.
Can anyone suggest any solution for this.
Is there any restriction in the length/size of the command that can be executed using Runtime.getRuntime.exec() method. or will it be a problem with unix ?
But when i execute the unix command directly in unix command prompt, then the mail is triggered to n number of recipients.

thanks in advance.
 
Bartender
Posts: 689
17
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When using exec you have to be aware of a number of gotchas. I suggest you read http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html.

If you still can't get it working post the code you are using so we can see what you are doing.

Edit: Too slow again.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If its any consolation I'm normally the one that is too slow.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nethaji Chandra babu wrote:I have a unix command and i am executing it from java using Runtime.getRuntime.exec() method.

Mike and Tony are giving you great advice, but my question is: Why would you use a Java program to run a Unix script?

There may well be a reason, but the general ethos of Java is to be platform-independent; so if you're using it to run a platform-specific command (that isn't even related to Java), you might want to ask yourself whether this is the best use of the language - especially since (presumably) users can either run the command directly themselves or you could create a desktop icon for it.

My 2 ¢

Winston
 
Nethaji Chandra babu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
First of all thanks for the replies.
Here is my code :

String commands []=new String[2];
commands [0] ="sh";
commands [1] = "/nova/prod/sendfile -f <from address> -t <mutliple to address> -c <multiple cc address> -s <mail subject>";
Process proc = Runtime.getRuntime().exec(commands);
InputStreamReader inputStreamReader=new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader (inputStreamReader);
String line;
while ((line = input.readLine ()) != null){
System.out.println(line);
}
proc.waitFor();
inputStreamReader.close();

The command is to trigger email. I am able to get the email when the length of the command is around 280.
if the length of the command exceeds (say 300) then i am not able to receive the email. it neither throws exception nor shows error in the command.
the sendfile is a perl script which sends the mail. the -f, -t, -c and -s are its arguments.
if i run the command "/nova/prod/sendfile -f <from address> -t <mutliple to address> -c <multiple cc address> -s <mail subject>" alone in unix command prompt, then i am able to receive the email irrespective of the command length

thanks
 
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first response to this thread Mike gave the URL of an article dealing with traps associated with Runtime.exec(). The article give a number of recommendations and you have not implemented all the recommendations. From what you have just said, I suspect that you have a deadlock associated with stderr or stdout that would be resolved if you implemented ALL the recommendations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!