我正在尝试编译,然后在Windows中通过命令行运行程序。
我有以下代码,我尝试了我评论的线程方法,然后是单一过程方法。生成类文件,并希望执行也生成hello world。如何在字符串中捕获它。请注意,这是我正在使用的Windows机器。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CompilerMain {
public String doCompile(String compilationLang, String codeString,
String challengeName) throws InterruptedException, IOException {
String output = null;
String commandString = null;
if (compilationLang == "java") {
String path = "c:\\Test\\";
challengeName = "Solution";
String commandStringCompile;
String commandStringExcecute;
commandStringCompile = "javac " + path + challengeName + ".java";
commandStringExcecute = "java " + path + challengeName;
System.out.println("Command is " + commandStringCompile);
System.out.println("Command is " + commandStringExcecute);
final Process p = Runtime.getRuntime().exec(commandStringCompile);
final Process q = Runtime.getRuntime().exec(commandStringExcecute);
Scanner err = new Scanner(p.getInputStream());
Scanner in = new Scanner(q.getInputStream());
while (in.hasNext()) {
System.out.println(in.next());
}
while (err.hasNext()) {
System.out.println(err.next());
}
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
/*
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
p.waitFor();
if(p.exitValue()==0){
System.out.println("Error! Code " + p.exitValue());
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
if(p.exitValue()!=0){
BufferedReader input = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
final Process q = Runtime.getRuntime().exec(commandString);
commandString = "java "+ challengeName + " >>out.txt";
new Thread(new Runnable() {
public void run() {
BufferedReader input = new BufferedReader(
new InputStreamReader(q.getInputStream()));
String line = null;
try {
while ((line = input.readLine()) != null) {
// System.out.println(" 1");
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
q.waitFor();
System.out.println("Exit Value is " + q.exitValue());
*/
}
return output;
}
}
答案 0 :(得分:1)
你的方法很好。可能你的命令字符串不正确。请尝试阅读 p.getErrorStream()和 q.getErrorStream(),您将看到发生了什么。
我稍微更改了你的代码,以便在我的Linux机器上工作。
public String doCompile(String compilationLang, String codeString,
String challengeName) throws InterruptedException, IOException {
String output = null;
String commandString = null;
if (compilationLang == "java") {
String path = "/home/john/IdeaProjects/Test1/src/";
String commandStringCompile;
String commandStringExcecute;
commandStringCompile = "javac " + path + "Main.java";
commandStringExcecute = "java -cp " + path +" Main";
System.out.println("Command is " + commandStringCompile);
System.out.println("Command is " + commandStringExcecute);
final Process p = Runtime.getRuntime().exec(commandStringCompile);
final Process q = Runtime.getRuntime().exec(commandStringExcecute);
Scanner err = new Scanner(p.getErrorStream());
Scanner in = new Scanner(p.getInputStream());
while (in.hasNext()) {
System.out.println(in.next());
}
while (err.hasNext()) {
System.out.println(err.next());
}
err = new Scanner(q.getErrorStream());
in = new Scanner(q.getInputStream());
while (in.hasNext()) {
System.out.println(in.next());
}
while (err.hasNext()) {
System.out.println(err.next());
}
}
return output;
}
输出:
Command is javac /home/john/IdeaProjects/Test1/src/Main.java
Command is java -cp /home/john/IdeaProjects/Test1/src/ Main
hello
world!