0

Here's my updated code. This is the button being clicked in the class I made separate form the java class you provided. I know it says Ping (disregard that, I'm using the button for testing purposes) I don't see how they would reference each other with the Process P line of code you provided. What do you think?

JButton btnPingComputer = new JButton("PING");

        btnPingComputer.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                String line;
                BufferedWriter bw = null;
                BufferedWriter writer =null;
                try {
                    writer = new BufferedWriter(new FileWriter(tempFile));
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                String lineToRemove = "OU=Workstations";
                String s = null;

                Process p = null;
                try {
                    p = Runtime.getRuntime().exec("cmd /c start c:\\computerQuery.bat computerName");
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {

                    p = Runtime.getRuntime().exec("c:\\computerQuery.bat");

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }
                StringBuffer sbuffer = new StringBuffer(); // new trial
                BufferedReader in = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));

                try {

                    while ((line = in.readLine()) != null) {

                        System.out.println(line);

                        textArea.append(line);
                        textArea.append(String.format("  %s%n", line));
                        String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
                        LdapName ldapName = new LdapName(dn);
                        String commonName = (String) ldapName.getRdn(ldapName.size() - 1).getValue();
                        System.out.println(commonName);
                    }

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                } catch (InvalidNameException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally

                {
                    try {
                        fw.close();

                    }

                    catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                try {

                    in.close();

                } catch (IOException e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            }

        });
1
  • You call the batch file with the parameter. How isn't this a duplicate of the last time you asked this? Commented Jun 13, 2015 at 16:13

1 Answer 1

2

Add the parameter to your java program like this :

Process p = Runtime.getRuntime().exec("cmd /c start c:\\batFile.bat computerName");
This will pass parameter_to_pass to the batch file.

For your situation this code should work well:

/*
This java program copies the value from a jTextField, adds it to a     predifined value 
and send it to command-line as a parameter. All these happens if you click     the jButton
*/


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class cmdJavaTest extends JFrame {
JTextField jTextField1 = new JTextField(20);
JButton jButton1 = new JButton("Click");
JLabel jLabel1 = new JLabel();

public cmdJavaTest() {
    super("CmdJavaParameterPass");

    getContentPane().setLayout(new FlowLayout());

    getContentPane().add(jTextField1);
    getContentPane().add(jButton1);
    getContentPane().add(jLabel1);
    jButton1.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            sendParam();
        }
        });
        setSize(300, 170);
        setVisible(true);
    }

  public void sendParam(){
      try{
            String val = "Computer"+jTextField1.getText(); //Put whatever you want to pass as a prefix in place of "Computer"
            jLabel1.setText(val);
            Process p ;
            p = Runtime.getRuntime().exec("cmd /c start c:\\batFile.bat "+val+"");
        }
        catch(Exception e){
            e.printStackTrace();
        }
  }

  public static void main(String argv[]) {
    new cmdJavaTest();
  }
}

Use this as you test batch file content
@dsquery computer -name %1 pause

But you must also see how to use a ProcessBuilder.

Thanks, hope it helps

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

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.

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.