Shell file to run other shell files running jar files

Hi,
I am trying to write a shell script that will go to another folder and run the script in that folder. The folder structure is kind of like this:

/MainFolder/
|-> MainShellScript.sh
|
|-> Folder1/
|-----|-> script1.sh
|-----|-> FileToRun1.jar
|
|-> Folder2/
|-----|-> script2.sh
|-----|-> FileToRun2.jar

So, trying to run MainShellScript.sh, which in turn will run the script1.sh and script2.sh, where these guys are running the jar files in their respective folders.

The code is as follows:

MainShellScript.sh:

#!/bin/bash

for d in */ ; do
    sh "$d"/script1.sh &
done

Script1.sh:

JAVA_EXE=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.9.x86_64/jre/bin/java
${JAVA_EXE} -Dsun.lang.ClassLoader.allowArraySyntax=true  -jar FileToRun1.jar > output.txt &

There are two problems:
1 - when I call the MainShellScript.sh, it does call the script1 and script2 successfully, but those scripts in turn cannot find the jar files. I get the following message:

Error: Unable to access jarfile FileToRun1.jar
Error: Unable to access jarfile FileToRun2.jar

2 - the script1 and script2 both are redirected to output.txt. When I call the MainShellScript.sh, it creates only one output.txt under the MainFolder/

Any advice would be greatly appreciated :slight_smile:
Thanks!

I don't use java but how about trying:-

${JAVA_EXE} -Dsun.lang.ClassLoader.allowArraySyntax=true  -jar /ABSOLUTE/PATH/TO/FileToRun1.jar > output.txt &

Also check the directory tree and file(s) have the correct permissions...

thanks wisecracker....but unfortunately it seems this is not working either. The java file in turn calls a properties file, which when run from the MainShellScript.sh throws a exception "java.io.FileNotFoundException".

From I suspect, it has to do with the scope of the shell script. When calling the MainShellScript.sh, it seems like the active current directory becomes the MainFolder's directory for all the subsequent shell scripts called.

Try

#!/bin/bash
P=$(pwd)
for d in */ ; do 
  cd $P/$d
  sh script?.sh &
  done
cd $P