Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Re : Run Linux Shell script in Java  RSS feed

 
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

 
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
 
Marshal
Posts: 64496
225
  • 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
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I'm so happy! And I wish to make this tiny ad happy too:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!