0

I need to run a shell script in java. The script accepts two parameters as an argument 1st is the name and the second is the directory path.

I'm using Windows as the operating system on my local machine.

Below is the code I'm trying to run:

ProcessBuilder processBuilder = new ProcessBuilder("D:\\temp\\script\\create_script.sh", name, sourceDir);     
       try {
            Process process = processBuilder.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                //abnormal...
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

The above code gives below error:

2021-05-12 22:08:47.383  INFO 10600 --- [nio-8090-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
java.io.IOException: Cannot run program "D:\temp\script\create_script.sh": CreateProcess error=193, %1 is not a valid Win32 application
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1128)
    at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1071)

Can someone please help me with the missing part?

Appreciate all your help! Thanks in advance!

Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36
  • 1
    Probably duplicate of https://stackoverflow.com/questions/12637203/why-does-createprocess-give-error-193-1-is-not-a-valid-win32-app ? Can you check please Sweta Sharma? – Sixro May 12 '21 at 16:50

2 Answers2

0

If you are using Windows, the problem might be that you are trying to run a Linux shell script (.sh), hence Windows doesn't really appreciate that. You can either translate Linux shell script into Windows bash script or try to run your program again on a Linux OS, which I would recommend the latter.

  • Thanks so much for your reply. However, When I run the script manually using a command prompt it executes the script. I'm curious to know the need of converting it to windows bash script. – Sweta Sharma May 12 '21 at 16:55
0

The below code worked for me!

ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command("cmd.exe", "/c", "D:\\temp\\script\\create_script.sh);
        try {
            Process process = processBuilder.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                System.exit(0);
            } else {
                //abnormal...
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
Sweta Sharma
  • 2,404
  • 4
  • 21
  • 36