1

I've found this topic, but the code doesn't work for me... Return Windows cmd text from Java?

After pressing a button I want to execute a batch-file, for testing purposes it's just the ipconfig-command.

The cmd-output should be written into a JTextFiled, but all I get is no text...

Here the code for writing it into the JTextField:

btnLock.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) 
        {       
            String g = "";
            try {
                Runtime.getRuntime().exec(new String[] {"ipconfig", g});
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  
            Process p = null;
            try {
                p = Runtime.getRuntime().exec(new String[] {"ipconfig", g});
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            InputStream s = p.getInputStream();

            BufferedReader in = new BufferedReader(new InputStreamReader(s));
            String temp;

            try {
                while ((temp = in.readLine()) != null) 
                {
                    System.out.println(temp);
                }
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

    });
    btnLock.setBounds(10, 68, 89, 23);
    contentPane.add(btnLock);

So what do I do wrong?

It's my first project with cmd-input, so please don't get mad cause of silly mistakes I made. ;)

Thx

3 Answers 3

3

Try the exec command that just takes a String parameter. The following test code worked on my system (though I was only printing to console, not to textfield):

BufferedReader in = null;
try{
    Process p = Runtime.getRuntime().exec("ipconfig");
    InputStream s = p.getInputStream();

    in = new BufferedReader(new InputStreamReader(s));
    String temp;

    while ((temp = in.readLine()) != null) {
        System.out.println(temp);
    }
} catch (Exception e){
    e.printStackTrace();
} finally {
    if (in != null) in.close();
}

Also your code in the original post is also using a System.out.println. As far as I'm aware, you can't print to a JTextField using System.out.println.... You'd have to use the setText method.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, it works fine with a non-gui-application. But if I try it with my GUI (and a JTextArea) the output is only the last line...
Well yes. It's in a while loop. You'll want to do something like String outpt = ""; while( (temp = ...)) { outpt += temp + "\n"; } textField.setText(outpt);
3

If I run

 ipconfig ""

I get

** Error: unrecognized or incomplete command line.**

You can only run from Java, commands which work on the command line.

BTW: If you are looking for errors, you need to read the error stream.

4 Comments

Thx, I'm new to Java, so please excuse, but what does "commands which work on the command line" mean? Why does ipconfig not work on the command line?
Explicitly, ipconfig "" does not work on the commandline. ipconfig does.
@user1318160 I suggest you try it on the command line for yourself. ;)
Oh, I didn't notice the "" , sorry!
1

I would Runtime.getRuntime().exec(new String[] {"ipconfig > temp.txt"}); and then just read it as a text file using a BufferedReader.

I hope this helps.

2 Comments

Do you have the whole code? I tried to do it, but I only get: Error: unrecognized or incomplete command line.
Ok, I used Roddy of the Frozen Pea's solution, works flawlessly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.