aspose file tools*
The moose likes Java in General and the fly likes Re : Run Linux Shell script in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Re : Run Linux Shell script in Java" Watch "Re : Run Linux Shell script in Java" New topic
Author

Re : Run Linux Shell script in Java

sam shaw
Greenhorn

Joined: Apr 24, 2009
Posts: 2
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

Joined: Mar 07, 2009
Posts: 120
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

Joined: Oct 13, 2005
Posts: 36578
    
  16
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

Joined: Apr 24, 2009
Posts: 2
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

Joined: Oct 13, 2005
Posts: 36578
    
  16
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Re : Run Linux Shell script in Java
 
Similar Threads
Compile and create instance of a new java program from existing java program
Running system commands
IOException while executing runtime.exec
Running unix command using java shows error
How to run .java file from Runnable