gpt4 book ai didi

java - 如何在Java编写的IDE中集成GCC编译器?

转载 作者:行者123 更新时间:2023-11-30 20:41:04 26 4
gpt4 key购买 nike

如果我用 java 编写自己的编辑器,那么是否可以运行用 C 编程语言编写的程序?如果是的话怎么办?

最佳答案

试试这个:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class C_Compile {
public static void main(String args[]){

String ret = compile();
System.out.println(ret);

}
public static String compile()
{
String log="";
String myDirectory = "C:\\";
try {
String s= null;
//change this string to your compilers location,Assume C file is in c:\\Hello.c
Process p = Runtime.getRuntime().exec("cmd /C gcc Hello.c", null, new java.io.File(myDirectory));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
boolean error=false;

log+="";
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;
}}

关于java - 如何在Java编写的IDE中集成GCC编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18780922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com