1

I am trying to loop into files, and run the script sh in each file using JAVA.

for (File file : files) {
            if (file.isDirectory()) {           
                    Runtime.getRuntime().exec("cmd.exe /c start "
                             +file.getAbsolutePath()+"\\creationNoPersisWF12.sh");
                        TimeUnit.SECONDS.sleep(1800);}}

The window of the script opens and get closed immediately. However, when I try the execute the scripts sh from outside, they execute successfully.

I need some help please.

LSoft
  • 87
  • 7

1 Answers1

0
    Process process = Runtime.getRuntime().exec("cmd.exe /c start "
                             +file.getAbsolutePath()+"\\creationNoPersisWF12.sh");
    if(process.exitValue()!=0){
        throw new RuntimeException(new String(process.getErrorStream().readAllBytes()));
    }

you have to check the exit value and read the error stream for the errors since its a process on its own, it won't throw any error on its own after invoking the process you have to check if it ran successfully with exitvalue 0 or not.

k4vin
  • 1,497
  • 10
  • 19
  • The project won't compile : The method readAllBytes() is undefined for the type InputStream – LSoft May 07 '20 at 09:32
  • @LSoft readAllBytes is available since Java 9, so it won't be available if you are on java 8 or lower. – lugiorgi May 07 '20 at 09:37
  • @LSoft https://stackoverflow.com/questions/309424/how-do-i-read-convert-an-inputstream-into-a-string-in-java – lugiorgi May 07 '20 at 09:43
  • I tried this int exitVal =process.waitFor(); System.out.println("Process exitValue: " + exitVal); The exit value is 1 – LSoft May 07 '20 at 09:45