When writing a program, you sometimes need to call the shell script in a Java program. You can use the exec method of runtime to call the shell program and run the script.
Each Java application hasRuntimeClass instance, so that the application can be connected to the running environment. You can use the runtime object to return the running environment, including the number of CPUs and the memory size of the virtual machine. You can also use the exec method to call and execute commands. You can usegetRuntimeMethod to obtain the current runtime instance.
public boolean ExeShell(){ Runtime rt = Runtime.getRuntime();
try { Process p = rt.exec(checkShellName);
if(p.waitFor() != 0)
return false;
} catch (IOException e) { Syslog. Error ("no detection script found "); return false;
} catch (InterruptedException e) { e.printStackTrace();
return false;
}
return true;
}
The P. waitfor () statement is used to wait for the completion of the sub-process. The return value is the Process Termination exit code.