I’m trying to execute a simple shell script from Java Process Builder class,
Shell Script
#!/bin/sh
touch test.txt
Java Code
import java.io.IOException;
public class Main {
public static void main(String[] args) {
// Specify the shell script file path
String scriptPath = "/Users/work/create.sh";
// Create a ProcessBuilder instance
ProcessBuilder processBuilder = new ProcessBuilder();
// Configure the ProcessBuilder to run the shell script
processBuilder.command("sh", "-c", scriptPath);
// Inherit the I/O of the current Java process
processBuilder.inheritIO();
try {
// Start the process
Process process = processBuilder.start();
// Wait for the process to complete and get the exit code
int exitCode = process.waitFor();
System.out.println("Exited with code: " + exitCode);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
The process is executing with exit code 0, but the file was not being created.
Thank you for the discussions ahead!