想从这个链接运行示例.cpp文件时编译和Java中运行C程序
Compile and run source code from Java application
我安装了MINGW32编译运行的源代码改变编译器的位置路径,并得到这个错误在Eclipse中。
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在你的PATH环境中(在shell中键入'gcc -v'应该打印类似'Target:mingw32'和版本的东西) - 你也可以简单地执行'cmd/C gcc'。 –
2013-02-12 06:50:33
我用gcc.exe仍然出现同样的错误: 进程p = Runtime.getRuntime().exec(“cmd/C”C:\\ MinGW \\ bin \\ gcc.exe \“Hello.cpp”) ; – 2013-02-12 06:50:52
*相同*错误?或者告诉你一个不同的错误,hello.cpp找不到?正如我所说的,你必须提供你的Hello.cpp路径(它不应该位于你的gcc的bin目录中!如果是这样 - 将它放在一个项目目录中) - 或者你提供的目录是exec-命令为[here](http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html#exec%28java.lang.String [],%20java.lang.String [],%20java.io.File%29):'Runtime.getRuntime()。exec(“cmd/C gcc hello.cpp”,null,new java.io.File(myDirectory));' – 2013-02-12 06:55:16