我已经返回了一个脚本,执行时会在一个文件中修改一行。
工作脚本的流程来自打印检查点111之后的run方法,打印检查点aaaa后将执行executeCmd方法,执行再次运行方法打印检查点222并返回executeCmd方法并在打印bbb后退出执行。
但在我的情况下,在checkpoint aaa之后它打印检查点bbb并且这个循环永远不会结束,因此执行不会返回run方法,因此脚本会卡住并挂起会话。
public String executeCmd(String classOpts, String cmdLine, String[] opts)
{
while (myCmd.isAlive() == true)
{
try
{
log.debug("checkpoint aaaa");
Thread.sleep(100);
log.debug("checkpoint bbbb");
}
}
exitVal = myCmd.getCmdExitValue();
log.debug("The script exit code: = " + exitVal);
}
public void run()
{
Runtime rt = Runtime.getRuntime();
try
{
String sCommand = cmdUtils.sScriptLauncher + " " + this.cmdline;
proc = rt.exec(cmdUtils.sScriptLauncher + " " + this.cmdline;
proc = rt.exec(cmdUtils.ParseCommandLine(sCommand));
try
{
log.debug("Checkpoint 111");
cmdExitVal = proc.waitFor();`enter code here`
log.debug("Checkpoint 222");
}
//remaining code
答案 0 :(得分:0)
我使用以下使用RunProcessCMD
,ProcessBuilder
和stdout
的课程stderr
:
class RunProcessCMD {
static BufferedReader stdout, stderr;
private Boolean isWaitFor = true;// wait for reply from CMD
private static RunProcessCMD startRunProcessCMD = null;
private String[] input = null;
private static boolean isRealTime = false;
private static StringBuilder buff = null;
public static RunProcessCMD getInstance(){
if(startRunProcessCMD == null){
startRunProcessCMD = new RunProcessCMD();
}
return startRunProcessCMD;
}
private RunProcessCMD(){}// destroy public constructor
public void start(String[] command) throws IOException {
buff = new StringBuilder();
System.out.println(Arrays.asList( command ) );
ProcessBuilder launcher = new ProcessBuilder();
launcher.redirectErrorStream(true);
launcher.command(command);
launcher.start(); // And launch a new process
buff.append("Done").append("\n");System.out.println("Done.");
}
public void start () throws IOException, InterruptedException{
buff = new StringBuilder();
if(input == null){
buff.append("Command == null");
return;
}
//String[] input = new String[] {"tasklist"};
Runtime r = Runtime.getRuntime();
//System.out.println("Execute ...");
//Process p = r.exec("cmd /c", input, null);
System.out.println(Arrays.asList( input ) );
Process p = r.exec(input);
//System.out.println("Finish to execute, start read output");
InputStream is = p.getInputStream();
stdout = new BufferedReader(new InputStreamReader(is));
is = p.getErrorStream();
stderr = new BufferedReader(new InputStreamReader(is));
//outputLines = new Vector();
if( isWaitFor == true ){
StdoutThread cltOut = RunProcessCMD.getInstance().new StdoutThread();
Thread tOut = new Thread(cltOut);
tOut.start();
StderrThread cltErr = RunProcessCMD.getInstance().new StderrThread();
Thread tErr = new Thread(cltErr);
tErr.start();
p.waitFor();
}
buff.append("Done").append("\n");System.out.println("Done.");
if( isWaitFor == false ){
buff.append("WaitFor defined to be false, respectivally no output from CMD").append("\n");
System.out.println("WaitFor defined to be false, respectively no output from CMD");
}
}
private class StdoutThread implements Runnable {
@Override
public void run() {
try {
int l;
String line;
for(l = 0; (line = stdout.readLine()) != null; ) {
if (line.length() > 0)
l++;
//outputLines.addElement(line);
buff.append(line).append("\n");
if(!line.trim().equals("")){
System.out.println(line);
}
}
stdout.close();
}
catch(IOException ie) {
buff.append("IO exception on stdout: " + ie).append("\n");
}
}
}
private class StderrThread implements Runnable {
public StderrThread() {}
@Override
public void run() {
try {
int l;
String line;
for(l = 0; (line = stderr.readLine()) != null; ) {
if (line.length() > 0) l++;
buff.append(line).append("\n");
System.out.print(line);
}
stderr.close();
}
catch(IOException ie) {
buff.append("IO exception on stdout: " + ie).append("\n");//System.out.println("IO exception on stdout: " + ie);
}
}
}
public static void ClearBuff (){
buff.setLength(0);
isRealTime = false;
}
public void setInput(String[] input) {
// reset flag
isWaitFor = true;
if(input[input.length-1].contains("waitFor") && input[input.length-1].split("=").length == 2 ){
String bull = input[input.length-1].split("=")[1];
isWaitFor = new Boolean( bull );
System.out.println("isWaitFor = " + isWaitFor);
// remove last value from String array
String[] input_new = new String[input.length -1];
for(int k=0; k < input_new.length; k++){
input_new[k] = input[k];
}
input = input_new;
}
// add proper value for input
String[] str = new String[input.length + 2];
str[0] = "cmd.exe";
str[1] = "/c";
for(int i=2; i<str.length; i++){
str[i] = input[i-2];
}
this.input = str;
}
public static StringBuilder getBuff() {
if( buff == null ){
return new StringBuilder( "" );
}
return buff;
}
public void setRealTime(boolean b) {
isRealTime = b;
}
}
<强>用法:强>
private void runSomeCommandOverCmd(){
String runP = "adb devices";
String[] strArr = new String[2];
strArr[0] = runP;
strArr[1] = "waitFor=true";
RunProcessCMD.getInstance().setInput( strArr );
try {
RunProcessCMD.getInstance().start();
String str = ( RunProcessCMD.getBuff() ).toString();
System.out.println(str);
RunProcessCMD.ClearBuff();
} catch (Exception e) {
try {
throw new Exception( "Failed to Start process CMD" );
} catch (Exception e1) {
e1.printStackTrace();
}
}
}