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!