Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Re : Run Linux Shell script in Java

 
sam shaw
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Hope all is well. I was trying to scratch my head here with simple problem of running Shell script in Java. I tried to google and look through forums but was unable to understand how to solve it.


I am using BASH and running on Redhat Linux with 1.4.2 JDK. Here is my simple Java class, which resides in different directory then my shell script. So I am trying to change path and run the shell script located there. Also, I am passing parameters to shell script from Java as arguments. But for some reasons ...I am getting IOException aand Java is unable to find my Shell script . Could you please suggest what I am I missing here.





Please let me know if you further need any information.

Thanks in advance,



Sam

 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec creates a new sub-process for every call, you invocation with “cd” command does not change the current working directory for the parent process and hence the second invocation inherits the working directory of parent.

Try using



and pass it a File object representing the desired working directory
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime.exec() is not at all simple to use. Google for "when Runtime.exec() won't" and you find the classic Michael Daconta paper about it.

By the way: Java 1.4 was superseded by Java 5 in September 2004, so why don't you upgrade?
 
sam shaw
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Runtime.exec() is not at all simple to use. Google for "when Runtime.exec() won't" and you find the classic Michael Daconta paper about it.

By the way: Java 1.4 was superseded by Java 5 in September 2004, so why don't you upgrade?


Hello All,

Thanks so much for all your help. It was indeed really helpful. I am bit novice when it comes to Java programming ...but have substantial backgroupnd in programming though. Yeah....I did find Runtime.exec() really difficult to understand...but again I am novice here to Java ...so can't really comment much. The reason for me to use 1.4.2 is that application that we run ( older version) only supports 1.4.2 or below. We are in process of upgrading application and hence will upgrade JDK too.

BTW, it was my mistake ( as usual ) .... my shell did not have execute rights on it and hence java was unable to trigger it...but surprisingly Java did not throw any exception at it ...is there anyway to trap this or again I am missing something here ?

Thanks again guys and enjoy sunny weekend.

- Sam.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic