想從這個鏈接運行示例.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