aspose file tools*
The moose likes Linux / UNIX and the fly likes Running .sh file through java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Running .sh file through java" Watch "Running .sh file through java" New topic
Author

Running .sh file through java

Ravindra Nandyala
Greenhorn

Joined: Jun 17, 2008
Posts: 7
Hi Alla,
I have java file(MsgHandler.java) under /opt/projects/java/com/interfaces/xyz directory and the shell script file(data.sh) is at /opt/projects/data_sh directory . I need to execute the data.sh shell script from my java file using exec() method of Process. When i tried to run the java file i got java.io.IOException: Data.sh: not found . I have set the classpath upto /opt/projects/data_sh directory. But still it is not able to recognize the data.sh file.


Please advise what can be done to solve this problem.

Thanks in anticipation.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Welcome to JavaRanch.

Adding the directory that contains the script to the classpath does not help. The classpath is used for finding Java classes, not for finding scripts or other executable files.

Can't you just pass the full path of the script to the exec() method?
[ June 27, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
data.sh java.io.IOException: Data.sh

Case issue?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
In perl and python its easier to do exec.

Im sure there is a perl/python way to do exec in java.

But what is it ?

Exec is generally complex because of the fact that java is cross platform.

but what if you KNOW your app will be deployed in unix/mac environments. Then is there a simpler shell script / perl type implementation of exec that behaves more predictably ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by jay vas:
Then is there a simpler shell script / perl type implementation of exec that behaves more predictably ?


Runtime.getRuntime().exec("data.sh") doesn't seem to difficult or complex to me. What's unpredicatable? The problem is that the Java program can't find your script. It might be because

1) The script is called data.sh but you're trying to run Data.sh (UNIX is case sensitive), or

2) The script isn't on the path

You haven't confirmed that the problem isn't one of these.


[Jess in Action][AskingGoodQuestions]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16142
    
  21

Or the script might not have its executable bit set.

However, it's generally not a good idea when mixing subsystems to specify a relative path (especially from a webapp, but also from cron jobs, etc.). Better to specify the absolute path on the script to be executed.


Customer surveys are for companies who didn't pay proper attention to begin with.
jay vas
Ranch Hand

Joined: Aug 30, 2005
Posts: 407
does Runtime.getRuntime().exec('');

work for any command ? even programs with input args ?

just checking.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by jay vas:
does Runtime.getRuntime().exec('');

work for any command ? even programs with input args ?

just checking.


Command line args? Yes, you just include the args on the command line. Input on standard input? You have to do something like (pseudocode):

Process p = ...exec(...);
OutputStream o = p.getOutputStream();
o.write("whatever");
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Tim Holloway:
Or the script might not have its executable bit set.


I think the error message would be different ("Cannot execute").


However, it's generally not a good idea when mixing subsystems to specify a relative path (especially from a webapp, but also from cron jobs, etc.). Better to specify the absolute path on the script to be executed.


Yes, absolutely (sorry )
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running .sh file through java