创建显示错误和输出的Java编译器
我试图创建一个程序,将Java代码编译成文本区码,然后运行它。我一直在将错误消息和运行创建的文件的结果输出到名为“Output”的JTextArea。我希望它像eclipse或JCreator一样,编译器或运行时错误显示在“输出”区域,System.out.println也在“输出”区域显示字符串。现在,当我输出运行该文件的结果时,我会在“output”文本区域(如java.io)中收到一条消息。FileInputStream@96e380c 任何帮助都将不胜感激。这是“运行”按钮的代码创建显示错误和输出的Java编译器,java,compiler-construction,Java,Compiler Construction,我试图创建一个程序,将Java代码编译成文本区码,然后运行它。我一直在将错误消息和运行创建的文件的结果输出到名为“Output”的JTextArea。我希望它像eclipse或JCreator一样,编译器或运行时错误显示在“输出”区域,System.out.println也在“输出”区域显示字符串。现在,当我输出运行该文件的结果时,我会在“output”文本区域(如java.io)中收到一条消息。FileInputStream@96e380c 任何帮助都将不胜感激。这是“运行”按钮的代码 btn
btnRun.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// write non-lambda file
try {
nonLambda = new File("nonLambda.java");
BufferedWriter writer = new BufferedWriter(new FileWriter(nonLambda));
writer.write(nLambdaTA.getText());
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
// compile non-lambda file
try {
Process compile = Runtime.getRuntime().exec("javac nonLambda.java");
output.setText(compile.getErrorStream().toString());
} catch(Exception e3) {
output.setText(e3.toString());
}
// run non-lambda file
try {
Process run = Runtime.getRuntime().exec("java -cp " +
nonLambda.getParent()+nonLambda.getName());
output.setText(run.getErrorStream().toString());
//output.append("\n"+run.getOutputStream().toString());
BufferedReader br = new BufferedReader(new InputStreamReader(run.getInputStream()));
String ch = new String();
while((ch=br.readLine())!=null)
output.append(ch+"\n");
} catch (Exception e4) {
output.append("\n" + e4.toString());
}
}
}) 进程同时运行,因此除非等待它们完成,否则不一定有任何输出,否则将有不完整的输出。这并不是你想象的那样: output.setTextcompile.getErrorStream.toString
在InputStream上执行toString不会耗尽流以生成字符串;它可能只是Object.toString。您的进程对象。一般的想法是让另外两个线程将输入流复制到内存中的缓冲区,甚至可能在主线程等待进程完成时实时更新UI。你能给我一些示例代码吗??以前从未使用过流程,今年在inputStreams上仅分配了1项任务。只是把它当作一个暑期项目