我们小组希望在java程序/应用程序中运行java源代码,因为里面的语法没有错误。怎么会这样?我们还需要编译错误吗?或正在编译不可避免的?谢谢......
就像netbeans可以在下面运行它的代码一样。
答案 0 :(得分:0)
答案 1 :(得分:0)
以下是如何使用Runtime
exec
方法从java代码运行java(或其他外部)程序,以及如何在执行期间读取命令的输出和可能的错误:
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "ps -ef" command
// using the Runtime exec method:
Process p = Runtime.getRuntime().exec("ps -ef");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}