• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running .sh file through java

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
data.sh java.io.IOException: Data.sh

Case issue?
 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic