• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running .sh file through java

 
Ravindra Nandyala
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
data.sh java.io.IOException: Data.sh

Case issue?
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does Runtime.getRuntime().exec('');

work for any command ? even programs with input args ?

just checking.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic