user1318160
user1318160

Reputation: 73

Windows cmd-output (Java)

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

Upvotes: 1

Views: 1975

Answers (3)

Roddy of the Frozen Peas
Roddy of the Frozen Peas

Reputation: 15180

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.

Upvotes: 3

Oday Mansour
Oday Mansour

Reputation: 234

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.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533500

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.

Upvotes: 3

Related Questions