Ken*_*yer 5 java compiler-construction
我需要从我的Java应用程序编译和运行用Python,Pascal或C编写的源代码(单个文件).
我需要知道:
我怎么能做到这一点?
我一直在做同样的事情..
public String compile()
{
String log="";
try {
String s= null;
//change this string to your compilers location
Process p = Runtime.getRuntime().exec("cmd /C \"C:\\Program Files\\CodeBlocks\\MinGW\\bin\\mingw32-g++.exe\" temp.cpp ");
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
boolean error=false;
log+="\n....\n";
while ((s = stdError.readLine()) != null) {
log+=s;
error=true;
log+="\n";
}
if(error==false) log+="Compilation successful !!!";
} catch (IOException e) {
e.printStackTrace();
}
return log;
}
public int runProgram()
{
int ret = -1;
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd.exe /c start a.exe");
proc.waitFor();
ret = proc.exitValue();
} catch (Throwable t)
{
t.printStackTrace();
return ret;
}
return ret;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 MiDE 中使用的两个函数,第一个函数用于编译。将地址更改为您的编译器位置。并返回日志(如果编译失败)以查看错误。
第二个运行编译后的代码。返回退出代码以检查它是否正确终止。
我不是一个很好的 java 程序员。我想你可以更好地改进我的代码;)..如果你这样做,请通知我。我也在寻找有关如何与创建的进程进行通信的答案
| 归档时间: |
|
| 查看次数: |
1449 次 |
| 最近记录: |