0

I am trying to execute a shell script (that makes use of a perl script and other shell scripts) from java program, however I am not successful, here is what I tried:

On the Linux server, in a folder test1/testJava the following scripts are available:

  1. decode24.sh (the main script I call from java program, this script makes use of the other scripts listed below)
  2. fram.sh
  3. shadow.pl
  4. light.sh

Here is what I try from java:

try {
        ConnBean cb = new ConnBean("xx.yyy.zz.p", "user","passme");
        ssh = SSHExec.getInstance(cb);
        CustomTask ct1 = new ExecShellScript("/test1/testJava", "./decode24.sh", "arg1");
        ssh.connect();
        net.neoremind.sshxcute.core.Result res = ssh.exec(ct1);
 }catch......

Result after execution:

error message:

./decode.sh[17]: shadow.pl: not found [No such file or directory]
./decode.sh[21]: fram.sh: not found [No such file or directory]
serenesat
  • 4,611
  • 10
  • 37
  • 53
  • The error you are getting comes from that shell script. Are you sure you are running it in the correct working directory and have you made sure that all files are executable? – GiantTree May 11 '15 at 13:44
  • What does your decode.sh look like? Probably you do not have `./` in front of the scripts you're trying to run and it cannot find them in the path anywhere – Eric Renouf May 11 '15 at 13:45

2 Answers2

1

The error comes from your decode.sh script. It cannot find the shadow.pl and fram.sh scripts that it executes. Probably because the CWD or path is not set to the script dir when you run the script.

The most robust way to solve it is to change your decode.sh script to use absolute paths to the shadow.pl and fram.sh scripts. That way you can execute decode.sh from any directory.

If you use bash, check out this question for a neat way to resolve the absolute directory to the scripts to avoid hard coding the path.

Community
  • 1
  • 1
K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
  • Thank you for the quick reply. However if for any reason I am unable to change the path to the absolute path on decode.sh, isn't there a way I can run decode.sh script from java? – Thinking May 11 '15 at 13:58
  • I know too little about the third party library you are using to execute the commands. Possibly you can provide an environment with a better PATH to it, or make it set CWD properly. However, the very best way is to make the shell scripts better and you will avoid future similar problems when using them. – K Erlandsson May 11 '15 at 14:06
0

Let decode.sh find out the directory it is in (see bash solution) and use the path for calling the others.

Community
  • 1
  • 1
Walter A
  • 19,067
  • 2
  • 23
  • 43