Question

I'm trying to create a console like java application to run other java program using Runtime.getRuntime.exec(). Input to the process is to be given from a JTextField.But the process is running before giving the input. How to make the process wait for input from the textfield?

package org.peditor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RunProg extends JFrame implements ActionListener {


private static final long serialVersionUID = 1L;
private String line="",editorString="";
final JTextField intext = new JTextField(25);
JTextArea outtext = new JTextArea(20,40);

public RunProg (String fileName){
    JPanel cp = new JPanel(new BorderLayout());
    intext.addActionListener(this);
    outtext.setEditable(false);
    intext.setEditable(true);
    JScrollPane sp = new JScrollPane(outtext);
    cp.add(sp,BorderLayout.WEST);
    cp.add(intext,BorderLayout.SOUTH);
    this.setContentPane(cp);
    this.setVisible(true);
    this.pack();
    this.setLocationRelativeTo(null);
    this.setTitle("*************OUTPUT CONSOLE***********");
    try
    {
        Runtime r = Runtime.getRuntime();
        Process p = null;
        //Stripping the file extension
        fileName = fileName.substring(0, fileName.lastIndexOf("."));            
        p = r.exec("java "+fileName);
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader errStream = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        OutputStream childin = p.getOutputStream();
        BufferedWriter childWriter = new BufferedWriter(new OutputStreamWriter(childin));
        childWriter.write(getInput());
        childWriter.newLine();
        childWriter.flush();
        childin.close();

        while ((line = inputStream.readLine())!=null) 
        {
            editorString = editorString+line+"\n";
            outtext.append(editorString);
        }
        while ((line = errStream.readLine())!=null)
        {
            editorString = editorString+line+"\n";
            outtext.append("ERROR"+"\n"+editorString);
        }
        p.waitFor();
        inputStream.close();
        errStream.close();
    }
    catch(Exception e1)
    {
        outtext.setText("Error running the program  "+e1.getMessage());
    }

}
public void actionPerformed(ActionEvent e) {
    getInput();

}
private String getInput() {
    String s = intext.getText();
    intext.setText("");
    outtext.append(s+"\n");
    return s;
}

}

Was it helpful?

Solution

You're calling the exec method in your Constructor. Obviously this will not work. You also have a random actionPerformed that does absolutely nothing. Remove all the Runtime stuff from your constructor, add a JButton, add an actionListener to that JButton and execute the Runtime code when they click that button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top