2013-02-12 86 views
1

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

回答

1

錯誤消息表明,沒有找到gcc編譯器本身。 爲什麼不使用gcc.exe而不是mingw32-gcc-4.6.2.exe呢?如果您更新了MinGW,則後者將失效! 你也不需要在字符串中使用\,當路徑中不包含空白字符時

你可以將你的cpp文件放在你想要的任何地方,提供該gcc的路徑.exec也應該有一個參數dir,你可以設置你的CPP的目錄。

+0

如果gcc在你的PATH環境中(在shell中鍵入'gcc -v'應該打印類似'Target:mingw32'和版本的東西) - 你也可以簡單地執行'cmd/C gcc '。 – 2013-02-12 06:50:33

+0

我用gcc.exe仍然出現同樣的錯誤: 進程p = Runtime.getRuntime().exec(「cmd/C」C:\\ MinGW \\ bin \\ gcc.exe \「Hello.cpp」) ; – 2013-02-12 06:50:52

+0

*相同*錯誤?或者告訴你一個不同的錯誤,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

0
public static void CompileCprog(String filename){ 

     File dir = new File("C://Users//JohnDoe//workspace//Project"); 

     try { 
      String exeName = filename.substring(0, filename.length() - 2); 
      Process p = Runtime.getRuntime().exec("cmd /C gcc " + filename + " -o " + exeName, null, dir); 
//   Process p = Runtime.getRuntime().exec("cmd /C dir", null, dir); 
      BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line = null; 
      while ((line = in.readLine()) != null) { 
       System.out.println(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

這完全適用於我。

的「目錄」變量可以設置爲你想要的任何位置。

的第一個「p」進程編譯一個程序並在你正在編譯的程序的同一位置產生一個具有相同名稱(減去.c)的.exe文件。

如果您的命令有輸出,則可以使用緩衝讀取器。 如果將命令字符串更改爲.exec(「cmd/C dir」);這個結果將被打印在輸出中。 (im使用eclipse)