0

i run a script shell from springboot application , it show this error when i run the spring application :

Cannot run program "src/main/java/com/springapp/app/bach/scripts/test.sh": CreateProcess error=193, %1 is not a valid Win32 application

#!/bin/bash

echo "Hello World !"

this is the controller which execute the commande :

    @Override
public String runScript(String pathScript) throws IOException, InterruptedException {
    try {
        Process p = Runtime.getRuntime().exec(pathScript);

        InputStream is = p.getInputStream();
        StringBuffer sb = new StringBuffer();


        int i = 0;
        while ((i = is.read()) != -1) {

            sb.append((char) i);

        }

        return sb.toString();
    }
    catch (IOException e){
        e.printStackTrace();
    }
}

the main applicattion :

    String message = runScript.runScript("src/main/java/com/innovsys/qopsbe/bach/scripts/test.sh");

    System.out.println(message);
 

I did my research on google i found a solution, they told me to put a "cmd" just before the path of the file

but it didn't work for me !!

Kenster
  • 23,465
  • 21
  • 80
  • 106
  • 2
    Does this answer your question? [How to run .sh on Windows Command Prompt?](https://stackoverflow.com/questions/26522789/how-to-run-sh-on-windows-command-prompt) – seenukarthi Mar 15 '22 at 08:18
  • Put the file in `src/main/resources` or even better not even in the `src/main` tree. That will be part of the classpath and thus internal to your application. Windows won't see it. It needs to be externally readable, so when packaged it isn't. Also anything in `src/main/java` that is non java isn't copied to the classpath so not available. – M. Deinum Mar 15 '22 at 08:58
  • @M.Deinum i created a path in `C:/Users` , same thing , it didn't show nothing – Iheb El Habib Mar 15 '22 at 19:22
  • It showed nothing, it should do something. Also you need to use the full path and make sure you are putting a `cmd` in front of it, else it probably won't work. Just noticed that it is a `.sh` file, which is not executable in windows (by default) and that is what the error is actually telling you! – M. Deinum Mar 15 '22 at 19:25
  • @M.Deinum this is the path i used `cmd C:/Users/ihebl/bash/demo.sh` – Iheb El Habib Mar 15 '22 at 19:28
  • @KarthikeyanVaithilingam unfortunately .. no – Iheb El Habib Mar 15 '22 at 19:29
  • It still is a script for a bash shell **not** for windows... So unless you run this on a Linux/Unix flavor or WSL(2) this simply won't work on plain windows. – M. Deinum Mar 15 '22 at 19:29
  • @M.Deinum , i installed VM Ubuntu on my pc, and i run my application it's work perfectly, however on the future the application will be running on unix server , so it's solved for the moment – Iheb El Habib Mar 16 '22 at 09:49

0 Answers0