我正在尝试从此链接运行源代码
Compile and run source code from Java application
我安装了 Mingw32 编译器,更改了编译器位置路径,在 Eclipse 中运行示例 .cpp 文件时出现此错误。
public class C_Compile {
public static void main(String args[]){
String ret = compile();
System.out.println(ret);
}
public static String compile()
{
String log="";
try {
String s= null;
//change this string to your compilers location
Process p = Runtime.getRuntime().exec("cmd /C \"C:\\MinGW\\bin\\mingw32-gcc-4.6.2.exe\" C:\\MinGW\\bin\\Hello.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;
}}
错误:
mingw32-gcc-4.6.2.exe: error: CreateProcess: No such file or directory
谁能告诉我将源 .cpp 文件放在哪里。谢谢
最佳答案
错误消息表明,找不到 gcc 编译器本身。
你为什么不使用 gcc.exe
而不是 mingw32-gcc-4.6.2.exe
呢?如果您更新 MinGW,后者将失效!
此外,当路径不包含空白字符时,您不需要在字符串中使用\"。
您可以将您的 cpp 文件放置在您想要的任何位置,并提供该 gcc 的路径。 Exec 还应该有一个参数 dir
,你可以将它设置为你的 cpp 的目录。
关于java - 用Java编译和运行C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14826814/