I want to execute a shell script from java. I have the next code
ProcessBuilder pb2 = null;
if (this.args.size() == 0) {
pb2 = new ProcessBuilder(file.getPath());
} else if (this.args.size() > 0) {
pb2 = new ProcessBuilder(file.getPath(), this.args.get(0));
}
Process p2;
p2 = pb2.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
log(line);
}
But I'm getting Permission denied. I have try execute the file with sh in the beginning, but the code it's not working.
ProcessBuilder pb2 = null;
if (this.args.size() == 0) {
pb2 = new ProcessBuilder("sh", file.getPath());
} else if (this.args.size() > 0) {
pb2 = new ProcessBuilder("sh", file.getPath(), this.args.get(0));
}
Process p2;
p2 = pb2.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
log(line);
}
I could give it permission with chmod a+x but the idea is to do it automatic.
Update: I tried the first code with file.setExecutable(true) but nothing happens, I don't get Permission Denied but the file is not executing