final Button compileButton = new Button(shell, SWT.NONE);
compileButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) throws IOException {
Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c javac"+"\""+filename+">tmp\" 2>&1");
hp.waitfor();
FileInputStream fis=new FileInputStream(tmp);
BufferedReader br1=new BufferedReader(new InputStreamReader(fis));
String s=null;
try{
while((s=br1.readLine())!=null){
text.append(s+"\r\n");
}
}请高手,达人们看看我该怎样实现?上面是我的代码,但是老是有问题,该怎样修改而达到目的啊。急切判回复。谢谢

解决方案 »

  1.   

    你是要输出.java的源程序?
    如果只是输出源程序,简单的读取文件就可以了,
    如果是要输出编译的结果,就类似与一个编译器的事情,就会复杂的多。
      

  2.   

    其实也不复杂吧,就是调用javac命令就可以了。不过编译不能通过
      

  3.   

    贴完整代码,描述你思考过程,以及疑问。“请高手,达人们看看我该怎样实现?”让人很fuck!
      

  4.   

    package test;import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.FileDialog;
    import org.eclipse.swt.widgets.MessageBox;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.widgets.ScrollBar;
    import org.eclipse.swt.awt.*;
    import java.io.*;
    import java.lang.*;import org.eclipse.swt.widgets.Dialog;
    public class alex { private Text text_1;
    private Text text;
    protected Shell shell;
     private String tmp;
    private String fn="";
    static String filename;
    /**
     * Launch the application
     * @param args
     */
    public static void main(String[] args) {
    try {
    alex window = new alex();
    window.open();
    } catch (Exception e) {
    e.printStackTrace();
    }
    } /**
     * Open the window
     */
    public void open() {
    final Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
    display.sleep();
    }
    } /**
     * Create contents of the window
     */
    protected void createContents() {
    shell = new Shell();
    shell.setSize(520, 512);
    shell.setText("SWT Application"); final Button loadButton = new Button(shell, SWT.NONE);
    loadButton.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent arg0) {
    //shell.setText("ilouyou");
    FileDialog dlg=new FileDialog(shell,SWT.OPEN);
    String filename=dlg.open();
    try
    {
    if(filename!=null)
    {
    FileInputStream fis=new FileInputStream(filename);
    text.setText("");
    BufferedReader br=new BufferedReader(new InputStreamReader(fis));
    String s=null;
    while((s=br.readLine())!=null){
    text.append(s+"\r\n");
    }
    }

    if (filename== null)
    {
        fn = filename;
            shell.setText(fn);
            MessageBox successBox = new MessageBox(shell);
            successBox.setText("信息");
                successBox.setMessage("打开文件成功!");
                successBox.open();
     }}
    catch(Exception e)
    {
         MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
             errorBox.setText("错误");
             errorBox.setMessage("打开文件失败!");
             errorBox.open();}
    }
    });
    loadButton.setText("load");
    loadButton.setBounds(46, 407, 48, 22); text = new Text(shell, SWT.MULTI);
    text.setBounds(10, 60, 237, 341);
            
    final Button compileButton = new Button(shell, SWT.NONE);
    compileButton.addSelectionListener(new SelectionAdapter(){
    public void widgetSelected(SelectionEvent arg0){
    Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);
    //hp.waitFor();
    BufferedInputStream fis=new BufferedInputStream(hp.getInputStream());
    BufferedReader br1=new BufferedReader(new InputStreamReader(fis));
    //String s=null;
    String s;
    //StringBuffer sb;
    //String tmp;
    //try{
    //while((tmp=br1.readLine())!=null)
    //{
       //sb.append(tmp);
       //sb.append("\n");
    //}
    try{
    while((s=br1.readLine())!=null){
    text.append(s+"\r\n");
    }
    }

     
    catch(Exception e)
    {
         MessageBox errorBox = new MessageBox(shell, SWT.ICON_ERROR);
             errorBox.setText("错误");
             errorBox.setMessage("打开文件失败!");
             errorBox.open();}
    }
    });

    compileButton.setText("compile");
    compileButton.setBounds(296, 407, 48, 22); text_1 = new Text(shell, SWT.MULTI);
    text_1.setBounds(275, 60, 227, 341); final Button runButton = new Button(shell, SWT.NONE);
    runButton.setText("run");
    runButton.setBounds(434, 407, 48, 22);
    //
    }
    //tmp="cmd.exe/c javac"+"\""+filename+">tmp\" 2>&1"
    }
    兄弟们帮忙分析一下哈,我要实现的就是在左边一个text框中load上.java代码,然后在右边的text框中实现编译和运行,把运行结果显示到右边的text文本框中,现在是load代码没有问题,就是编译这边出现问题,具体可能就是在Process hp=Runtime.getRuntime().exec(tmp="cmd.exe/c java";);出现了问题。请求解决方案。
      

  5.   

    你的tmp ="cmd.exe/c javac"+"\""+filename+" >tmp\" 2 >&1"然后
    FileInputStream fis=new FileInputStream(tmp); 
    能用这个tmp生成 输入流吗?
    怀疑FileInputStream fis=new FileInputStream(="cmd.exe/c javac"+"\""+filename+" >tmp\" 2 >&1" //??????
    ); 
      

  6.   

    其实没什么,只是利用了jdk\bin里面的两个可执行文件。文件编译已经实现了,而观察运行结果时,看不到结果,我在控制台尝试编译了下,抛出了找不到main函数的异常。这个是环境变量Path的设置问题,应该不需要多少了。我把文件写死了,调试时,请确认d:\Alex.java确实存在。而你在上面文本框输入时,请填写准确的文件路径。