-3

I am trying to make it so that when a user clicks a button, a new screen appears and automatically runs a command line process, and they are able to see the outputs of this process.

I thought that I might be able to use a JTextArea to set text to.

Here's what I've got at the moment:

runButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent r)
            {
                JFrame runFrame = new JFrame("Running process...");
                runFrame.setVisible(true);
                runFrame.setSize(500, 400);
                runFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
                runFrame.setLayout(null);
                JTextArea run = new JTextArea();
                run.setBounds(100,50,300,200);
                runFrame.add(run);
                Runtime runtime = Runtime.getRuntime();
                Process process = null;
                try 
                {
                    process = runtime.exec("cat /cmd/h:/testfile");
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                InputStream runStream = process.getInputStream();
                InputStreamReader runStreamReader = new InputStreamReader(runStream);
                BufferedReader br = new BufferedReader(runStreamReader);
                String line;
                StringBuilder sb = new StringBuilder();
                try 
                {
                    while ((line = br.readLine()) != null)
                    {
                        sb.append(line);
                    }
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                run.setText(sb.toString());
            }
        });

The error I'm getting with this is:

Cannot run program "cat": CreateProcess error=2, The system cannot find the file specified

I was trying to test opening a file to test this, which just contains lines of random letters.

EDIT:

I'm not sure I explained clearly what I need this to do. What I want is for a command to be run in command line that opens a file. I then want the result of the command line to be output into the JTextArea.

EDIT 2:

I have tried to change my command to "ping riot.de -t". This will ping riots server every so often and it returns a response with the response time.

Upon running this, the new frame is just black, and java freezes up.

Plumel
  • 45
  • 9

1 Answers1

0

maybe the env of your java application execution has not the right path of cat cmd? you have try to specify absolute path for cat command?

aurox
  • 107
  • 1
  • 3
  • 8
  • How would I go about specifying an absolute path? – Plumel Sep 19 '16 at 08:54
  • Open a terminal and with command: which cat get absolute path of cat command. Try to use that one. I hope I was helpful – aurox Sep 19 '16 at 10:46
  • Thanks, I will try this. – Plumel Sep 19 '16 at 10:50
  • You try to run your command cat /cmd/h:/testfile on terminal? works? maybe the dirname "h:" could give any problem... – aurox Sep 19 '16 at 10:56
  • It came back saying that cat is not recognized as an internal or external command, operable program or batch file. I have changed the command to "ipconfig" and now it is returning correct information – Plumel Sep 19 '16 at 10:57