0

我想编译输入文本并使用托管 bean 和页面 .xhtml 创建 .exe 文件。然而,文件的创建顺利,.exe 的创建在这里不起作用是托管 bean 代码:

package Servlets;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
@ManagedBean(name = "fichier", eager = true)
public class Fichier implements Serializable{
private String code;
private String error;




public String getCode() {
    return code;
}

public String getError() {
    return error;
}
 public void setCode(String code) {
    this.code = code;
}

public void setError(String error) {
    this.error = error;
}



public void compile() throws IOException 
{


    File file = new File ("C:\\Users\\Rad1\\test.c");
    PrintWriter ecrivain;
    ecrivain =  new PrintWriter(new BufferedWriter (new FileWriter(file)));
    ecrivain.println(code);
            System.out.print("ecrivain");
    ecrivain.close();
    try {  
        Runtime runtime = Runtime.getRuntime();
         String[] cmd={"cmd.exe","/C gcc C:\\Users\\Rad1\\test.c -o C:\\Users\\Rad1\\test.exe"};
         Process p = runtime.exec(cmd,null,null );
                     System.out.print("boucle try");
          try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));

            try {

            String line=null;

            while((line = reader.readLine()) != null) {

                 error =error+ line + "\n";
                }
            } finally {
                reader.close();
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();

        }


        } 
    catch (Exception e) {System.out.println("erreur d'execution"); }




}

}

4

1 回答 1

0

您可能希望将“/C”作为单独的参数:

String[] cmd={"cmd.exe","/C", "gcc C:\\Users\\Rad1\\test.c -o C:\\Users\\Rad1\\test.exe"};

这就是它应该工作的方式,但我在 linux 上,现在无法测试 Cmd。

编辑:为什么runtime.exec(cmd,null,null)上有额外的null?

于 2013-04-30T00:45:26.103 回答