• Post Reply Bookmark Topic Watch Topic
  • New Topic

Executing sh command from Java  RSS feed

 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm generating a tar file from my java program which is executed from database(Oracle 10g).I'm using the Runtime.
aixCommand ="tar -cvf ccc.tat ccc.dat"
String[] aixCmds = new String[]{"sh", "-c", aixCommand };
I'm getting the following Exception :
java.io.IOException: can't exec:sh doesn't exist

Please help me out
Thanks in advance
Sujith
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec doesn't create the same kind of environemnt you have at the command line. Try something like "/usr/bin/sh" instead of "sh". It might also be better to break up the tar command into its separate pieces, so that the overall command is something like

new String[] {"sh", "-c", "tar", "-cvf", "ccc.tat", "ccc.dat" }

Here is an article that explains some of the pitfalls of using Runtime.exec.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dittmer,

As per your advice I used "/bin/sh"(as I located sh in /bin instead of /usr/bin) instead of "sh" and also broke the tar commang into separate pieces.I got no error but the tar file is not getting generated.But when run the same tar command on linux it's working fine.

Thanks,
Sujith
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is true for sh (you have to give the whole path) is true for tar too.
 
Sujith Kanaparthi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephen,

Thanks for your advice.I'm sure I can run the tar command from my programm.

Regards,
Sujith
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!