1

I have this MCVE for an Java class calling a bash script:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Test
{
        static BufferedReader in;
        public static void main(String[] args) throws Exception
        {
                String[] cmd = new String[]{"/bin/sh", "/usr/myapp/myscript.sh", "parameter1"};
                Process pr = Runtime.getRuntime().exec(cmd);
                in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
                String line = in.readLine();
                while(line != null)
                {
                        System.out.println(line);
                        line = in.readLine();
                }

        }
}

When I have the compiled .class file in the same directory as myscript.sh, it works just fine. As soon as I move the .class file to another folder, it doesn't execute the script anymore, although I am still using the absolute path to the script.

I tested this with JDK 1.8 on a BeagleboneBlack running Angstrom if this information is good for something.

How can I run the script, although it is in a different location?

mxcd
  • 1,954
  • 2
  • 25
  • 38

1 Answers1

1

Using the getErrorStream hint by Samuel really helped.

It was clear, that some sub-scripts that were in the same folder as the original shell script were not found.

The solution was as easy as using absolute paths to sub-scripts as well since the working directory is not the one of the called script but the one of the calling application (in my case the Java App)

mxcd
  • 1,954
  • 2
  • 25
  • 38
  • 1
    Besides using absolute paths for sub-scripts, you could also try using the script *dir*, like this: `$(dirname "$0")/subscript`. – Paulo Mattos May 22 '17 at 03:17