【问题标题】:Implementation of simple Java IDE using Runtime Process and JTextArea使用 Runtime Process 和 JTextArea 实现简单的 Java IDE
【发布时间】:2023-03-11 02:38:01
【问题描述】:

我正在开发一个简单的 Java IDE,例如 Netbeans/Eclipse。我的 GUI 包括两个 JTextArea 组件,一个用作 TextEditor,最终用户可以在其中键入他的程序,另一个用作输出窗口。

我通过 Java Runtime 和 Process 类调用 windows 命令提示符来运行用户程序。我还使用 getInputStream()、getErrorStream()、getOutputStream() 方法捕获进程的 IO 流。

如果程序只包含将某些内容打印到屏幕上的语句,我可以在输出窗口(JTextArea)上显示输出。但如果它包含从用户那里读取输入的语句,那么用户必须可以通过输出窗口键入预期的输入值,并且必须像在 Netbeans/Eclipse 中一样将其发送到进程。

我还检查了以下链接 java: work with stdin/stdout of process in same time

使用这段代码,我可以只显示等待输入的语句,而不是简单的输出语句。此外,一次只在输出窗口上显示一行。

如果有人能帮我解决这个问题,那就太好了。

谢谢

哈莉玛

【问题讨论】:

  • “像 Netbeans/Eclipse 这样的简单 Java IDE”:)

标签: process io runtime jtextarea


【解决方案1】:

我已经找到了对之前的帖子 java:work with stdin/stdout of the same time

稍加修改的解决方案
class RunFile implements Runnable{

    public Thread program = null;
    public Process process = null;

    private JTextArea console; 
    private String fn;
    public RunFile(JTextArea cons,String filename){ 
        console = cons;
        fn=filename;
        program = new Thread(this);
        program.start();
    }

    @Override
    public void run() {     
        try {  
String commandj[]  =  new String[4];
commandj[0] = "cmd";
commandj[1]="/C";
commandj[2]="java";
commandj[3] = fn;

String envp[] = new String[1];
envp[0]="path=C:/Program Files (x86)/Java/jdk1.6.0/bin";
File dir = new File("Path to File");

Runtime rt = Runtime.getRuntime();
process = rt.exec(commandj,envp,dir);

            ReadStdout read = new ReadStdout(process,console); 
            WriteStdin write = new WriteStdin(process, console);

            int x=process.waitFor();  


            console.append("\nExit value: " + process.exitValue() + "\n");  
        }
        catch (InterruptedException e) {} 
        catch (IOException e1) {}       
    }
}


class WriteStdin implements Runnable{

    private Process process = null;
    private JTextArea console = null;
    public Thread write = null;
    private String input = null;
    private BufferedWriter writer = null;

    public WriteStdin(Process p, JTextArea t){

        process = p;
        console = t;
        writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));

        write = new Thread(this);
        write.start();

        console.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyTyped(java.awt.event.KeyEvent e){

                //save the last lines for console to variable input
                if(e.getKeyChar() == '\n'){

                    try {                  

                        int line = console.getLineCount() -2;
                        int start = console.getLineStartOffset(line);
                        int end = console.getLineEndOffset(line);

                        input = console.getText(start, end  - start);

                        write.resume();

                    } catch (BadLocationException e1) {}
                }
            }
        });
        console.addCaretListener(new javax.swing.event.CaretListener() {

            @Override
            public void caretUpdate(CaretEvent e) {
           console.setCaretPosition(console.getDocument().getLength());     
                throw new UnsupportedOperationException("Not supported yet.");

            }
        });

        console.addFocusListener(new java.awt.event.FocusAdapter() {
            @Override
        public void focusGained(java.awt.event.FocusEvent e)
        {
                              console.setCaretPosition(console.getDocument().getLength());     
         }    

        });
    }


    @Override
    public void run(){
        write.suspend();
        while(true){
            try {
                //send variable input in stdin of process
                writer.write(input);
                writer.flush();

            } catch (IOException e) {}
            write.suspend();
        }
    }
}



class ReadStdout implements Runnable{

    public Thread read = null;
    private BufferedReader reader = null;
    private Process process = null;
private JTextArea console = null;
    public ReadStdout(Process p,JTextArea t){

        process = p;
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
console = t;
        read = new Thread(this);
        read.start();
    }

    public void run() {
        String line;
        try {
        while((line = reader.readLine())!=null)                       
          console.append(line+"\n");
             }catch (IOException e) {}

    }
}

【讨论】:

    猜你喜欢
    • 2017-01-12
    • 2018-06-21
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多