带有process.exec的JAVA并发stdInput和stdError
我试图在进程运行时从进程中读取stdInput和stdError。我创建了一个线程来运行这个进程,这很有效。在的构造函数中,我传递一个对已创建进程的引用,以便可以查看该进程的输出。问题是它似乎没有链接引用进程,而将其设置为null。所有这一切的目标是能够在进程仍在运行时观察进程的输出,目前我只能在进程完成后观察输出。我想这是由于胎面正在运行,但我希望有办法做到这一点带有process.exec的JAVA并发stdInput和stdError,java,multithreading,process,Java,Multithreading,Process,我试图在进程运行时从进程中读取stdInput和stdError。我创建了一个线程来运行这个进程,这很有效。在的构造函数中,我传递一个对已创建进程的引用,以便可以查看该进程的输出。问题是它似乎没有链接引用进程,而将其设置为null。所有这一切的目标是能够在进程仍在运行时观察进程的输出,目前我只能在进程完成后观察输出。我想这是由于胎面正在运行,但我希望有办法做到这一点 try { Process p = null; Thread mythread = n
try {
Process p = null;
Thread mythread = new Thread( new SJ(pathF.getText(), fileF.getText(), p, view) ) ;
mythread.start();
BufferedReader stdInput = null;
BufferedReader stdError = null;
String s = null;
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while (mythread.isAlive()) {
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
}
} catch ( IOException e ) {}
class SJ implements Runnable {
String Path = "";
String File = "";
public static viewering view;
Process p = null;
public SJ ( String arg1, String arg2, Process p1, viewering view ) {
Path = arg1;
File = arg2;
p = p1;
}
public void run() {
String[] command = new String[5];
command[0] = "cmd";
command[1] = "/C";
command[2] = "compile";
command[3] = Path;
command[4] = File;
BufferedReader stdInput = null;
BufferedReader stdError = null;
String s = null;
try {
p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
} catch ( IOException e ) {}
}
}
下面是最终工作场景的代码
public void actionPerformed(ActionEvent ae) {
frame.setVisible( false );
final Thread mythread = new Thread( new SJ(pathF.getText(), fileF.getText(), view) ) ;
mythread.start();
final Timer ThreadTimer = new Timer( 1000, null );
ThreadTimer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!mythread.isAlive()) {
frame.setVisible( true );
ThreadTimer.stop();
}
}});
ThreadTimer.start();
}
class SJ implements Runnable {
String Path = "";
String File = "";
public static viewering view;
public SJ ( String arg1, String arg2, viewering view1 ) {
Path = arg1;
File = arg2;
view = view1;
}
public void run() {
String[] command = new String[5];
command[0] = "cmd";
command[1] = "/C";
command[2] = "compile";
command[3] = Path;
command[4] = File;
BufferedReader stdInput = null;
BufferedReader stdError = null;
Process p = null;
String s = null;
try {
p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
} catch ( IOException e ) {}
}
}
好吧,这是一个完全不同的问题。现在我明白你真正的问题了。如果在按钮的单击侦听器中执行代码的第一部分,则在自己的线程中运行的事件循环将挂起一段时间。那你得用一根线 请看这个问题的最后一个答案:
p==null:您只能通过值将p=null传递给可运行的构造函数。即使您在runnable中创建进程并将对象分配给runnable中的p,外部p仍然为null。这将引发空指针异常。您不需要线程!你开始一个独立的过程。我不确定你在说什么,我只是尝试了p==p1;和进程p==null;两人都说他们不是指挥官。我该怎么办?关于这个问题,你不需要一根线!注释直到过程完成,exec的输出才会出现,我希望它是实时的,或者只是有点慢。对不起,我的答案不清楚。我使用p==null表示变量outer p被赋值为null。然后将该null传递给变量p1中的SJ构造函数。将p1分配给SJ对象中声明的内部p,p=p1;在run方法中,启动流程。它为您创建一个流程对象,该对象被指定给内部p。外部p仍然为空。我看不到将Process对象分配给外部p的方法。当您使用外部p时,它是空的。好的,我有一个工作场景,我正在发布完成的代码,以便其他人可以使用它来拥有一个接近实时的swing控制台端口。