我想通過javacode編譯C程序。我做了如下。通過javacode編譯C源碼
Process process = Runtime.getRuntime().exec("C:/cygwin/bin/sh -c 'gcc HelloWorld.c -o HelloWorld.exe'");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line, log;
log ="\n..Input..\n";
while ((line = br.readLine()) != null) {
log+=line;
log+="\n";
}
InputStream is2 = process.getErrorStream();
InputStreamReader isr2 = new InputStreamReader(is2);
BufferedReader br2 = new BufferedReader(isr2);
String line2;
log+="\n..Error..\n";
while ((line2 = br2.readLine()) != null) {
log+=line2;
log+="\n";
}
System.out.println(log);
HelloWorld.exe未創建並顯示以下錯誤消息。在/ usr/bin/sh的:GCC:找不到命令
這沒有幫助。閱讀javadoc中的'exec(String)'以瞭解它如何將字符串拆分爲參數。 –