java trace process_java - 使用Java实时获取进程输出 - 堆栈内存溢出

似乎您正在处理多线程问题,而不是获取进程的输出。

我刚刚制作了这个演示类,您可以使用:

CommandExecTest.java

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.util.ArrayList;

public class CommandExecTest {

public static void main(String[] args) throws InterruptedException {

String executable = "cmd";

String[] commandParams = {"@ping -n 5 localhost","echo \"hello world\"","exit 123"};

boolean passCommandsAsLinesToShellExecutableAfterStartup = true;

AsyncExecutor asyncExecutor = new AsyncExecutor(executable, commandParams,passCommandsAsLinesToShellExecutableAfterStartup);

System.out.println("x"+"/x\tsecs in main thread \t\t status:"+asyncExecutor.runstate+" of async thread that monitors the process");

asyncExecutor.start();//start() invokes the run() method as a detached thread

for(int i=0;i<10;i++) {

// you can do whatever here and the other process is still running and printing its output inside detached thread

Thread.sleep(1000);

System.out.println(i+"/10\tsecs in main thread \t\t status:"+asyncExecutor.runstate+" of async thread that monitors the process");

}

asyncExecutor.join(); // main thread has nothing to do anymore, wait till other thread that monitor other process finishes as well

System.out.println("END OWN-PROGRAMM: 0 , END OTHER PROCESS:"+asyncExecutor.processExitcode);

System.exit(0);

}

}

运行状态

public static enum Runstate {

CREATED, RUNNING, STOPPED

}

AsyncExecutor.java

public static class AsyncExecutor extends Thread{

private String executable;

private String[] commandParams;

public ArrayList linesSoFarStdout = new ArrayList<>();

public ArrayList linesSoFarStderr = new ArrayList<>();

public Runstate runstate;

public int processExitcode=-1;

private boolean passCommandsAsLinesToShellExecutableAfterStartup = false;

public AsyncExecutor(String executable, String[] commandParams) {

this.executable=executable;

this.commandParams=commandParams;

this.runstate=Runstate.CREATED;

this.passCommandsAsLinesToShellExecutableAfterStartup=false;

}

/**

* if you want to run a single-process with arguments use false example executable="java" commandParams={"-jar","myjarfile.jar","arg0","arg1"}

*

* if you want to run a shell-process and enter commands afterwards use true example executable="cmd" commandParams={"@ping -n 5 localhost","echo \"hello world\"","exit 123"}

* @param executable

* @param commandParams

* @param passCommandsAsLinesToShellExecutableAfterStartup

*/

public AsyncExecutor(String executable, String[] commandParams, boolean passCommandsAsLinesToShellExecutableAfterStartup) {

this.executable=executable;

this.commandParams=commandParams;

this.runstate=Runstate.CREATED;

this.passCommandsAsLinesToShellExecutableAfterStartup=passCommandsAsLinesToShellExecutableAfterStartup;

}

@Override

public void run() {

this.runstate=Runstate.RUNNING;

// 1 start the process

Process p = null;

try {

if(passCommandsAsLinesToShellExecutableAfterStartup) {

// open a shell-like process like cmd and pass the arguments/command after opening it

// * example:

// * open 'cmd' (shell)

// * write 'echo "hello world"' and press enter

p = Runtime.getRuntime().exec(new String[] {executable});

PrintWriter stdin = new PrintWriter( p.getOutputStream());

for( int i = 0; i < commandParams.length; i++) {

String commandstring = commandParams[i];

stdin.println( commandstring);

}

stdin.close();

}

else {

// pass the arguments directly during startup to the process

// * example:

// * run 'java -jar myexecutable.jar arg0 arg1 ...'

String[] execWithArgs = new String[commandParams.length+1];

execWithArgs[0] = executable;

for(int i=1;i<=commandParams.length;i++) {

execWithArgs[i]=commandParams[i-1];

}

p = Runtime.getRuntime().exec( execWithArgs);

}

// 2 print the output

InputStream is = p.getInputStream();

BufferedReader br = new BufferedReader( new InputStreamReader( is));

InputStream eis = p.getErrorStream();

BufferedReader ebr = new BufferedReader( new InputStreamReader( eis));

String lineStdout=null;

String lineStderr=null;

while(p.isAlive()) {

Thread.yield(); // *

// * free cpu clock for other tasks on your PC! maybe even add thread.sleep(milliseconds) to free some more

// * everytime this thread gets cpu clock it will try the following codeblock inside the while and yield afterwards for the next time it gets cpu-time from sheduler

while( (lineStdout = br.readLine()) != null || (lineStderr = ebr.readLine()) != null) {

if(lineStdout!=null) {

System.out.println(lineStdout);

linesSoFarStdout.add(lineStdout);

}

if(lineStderr!=null) {

System.out.println(lineStderr);

linesSoFarStderr.add(lineStderr);

}

}

}

// 3 when process ends

this.processExitcode = p.exitValue();

}

catch(Exception e) {

System.err.println("Something went wrong!");

e.printStackTrace();

}

if(processExitcode!=0) {

System.err.println("The other process stopped with unexpected existcode: " + processExitcode);

}

this.runstate=Runstate.STOPPED;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值