Method of Java calling shell script to solve the problem of parameter transfer and authority
- 2021-07-07 07:39:12
- OfStack
1. java Executes shell
java executes the command or script of shell through the Runtime. getRuntime (). exec () method, and the parameters of the exec () method can be the path of the script or the direct shell command
The code is as follows (this code is problematic. See 2) for the complete code:
/**
* Execute shell
* @param execCmd Use commands Or Script flag bit
* @param para Pass in parameters
*/
private static void execShell(boolean execCmd, String... para) {
StringBuffer paras = new StringBuffer();
Arrays.stream(para).forEach(x -> paras.append(x).append(" "));
try {
String cmd = "", shpath = "";
if (execCmd) {
// Command mode
shpath = "echo";
} else {
// Script path
shpath = "/Users/yangyibo/Desktop/callShell.sh";
}
cmd = shpath + " " + paras.toString();
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
2. Problems encountered and solutions
Parameter passing problem, when the parameter string passed contains spaces, the above method will truncate the parameters, and the default is that the parameters only go to spaces. Resolution: Place the shell command or script and parameters in an array, and then pass the array into the exec () method. Permission problem, when we use this. getClass (). getResource ("/callShell. sh"). getPath () to get the script position, the shell script under target is taken, and shell script has no execution permission at this time. Resolution: Before executing the script, give the script execution permission.The complete code is as follows
/**
* Solved Parameter contains Spaces and scripts do not have execution permission
* @param scriptPath Script path
* @param para Array of parameters
*/
private void execShell(String scriptPath, String ... para) {
try {
String[] cmd = new String[]{scriptPath};
// In order to solve the problem of containing spaces in parameters,
cmd=ArrayUtils.addAll(cmd,para);
// Resolution script does not have execute permission
ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755",scriptPath);
Process process = builder.start();
process.waitFor();
Process ps = Runtime.getRuntime().exec(cmd);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
// Execution results
String result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
Source Location:
https://github.com/527515025/JavaTest/tree/master/src/main/java/com/us/callShell
Reference://www.ofstack.com/article/61529. htm
Summarize