将Java变量添加到批处理文件

用户名

这是我更新的代码。这是我在与您提供的Java类分开的类中单击的按钮。我知道它说的是Ping(无视我正在使用按钮进行测试),我看不到它们如何与您提供的Process P行代码相互引用。你怎么看?

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();

                }

            }

        });
野马DC

像这样将参数添加到您的java程序中:

Process p = Runtime.getRuntime().exec("cmd /c start c:\\batFile.bat computerName");
这将传递parameter_to_pass到批处理文件。

对于您的情况,此代码应该可以正常工作:

/*
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();
  }
}

在测试批处理文件内容时使用它
@dsquery computer -name %1 pause

但是,您还必须了解如何使用ProcessBuilder

谢谢,希望对您有所帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将批处理文件添加到PATH

来自分类Dev

批处理文件以将Java密码学扩展文件添加到更新的Java

来自分类Dev

使用批处理文件中的msqldump将时间戳记添加到文件名

来自分类Dev

批处理文件以将行添加到FTP上的CSV文件

来自分类Dev

批处理文件 - 将文件名添加到 txt 的第一行

来自分类Dev

将分钟(整数)及时添加到批处理文件中

来自分类Dev

如何将文件夹名称添加到批处理文件中正在创建的文件中?

来自分类Dev

创建批处理文件,以文件名中的某些字符串将文件添加到数组

来自分类Dev

使用批处理文件将父目录名称作为前缀添加到子目录文件

来自分类Dev

批处理文件,将日期和时间添加到输出txt文件的每一行的开头

来自分类Dev

使用批处理文件将文本添加到文件名末尾(但在扩展名之前)

来自分类Dev

如何使用批处理文件将文本添加到多个文本文件的开头?

来自分类Dev

如何将批处理文件添加到Visual Basic中并按一个按钮执行?

来自分类Dev

批处理文件-将计算的日期添加到文件末尾

来自分类Dev

批处理文件-将计算的日期添加到文件末尾

来自分类Dev

批处理文件-循环以将字符串追加到变量

来自分类Dev

批处理文件可将数字前缀添加到基于月份字符串的文件名中

来自分类Dev

如何在Windows批处理文件中将后缀添加到ffmpeg输出的重复文件名?

来自分类Dev

批处理文件将“ =”替换为“?” 在文件或变量中

来自分类Dev

在嵌入式XP中将基本日期格式添加到批处理文件中

来自分类Dev

为什么Wallpaper.reg在添加到批处理文件时不执行其应做的工作?

来自分类Dev

将变量传递到批处理文件中

来自分类Dev

批处理文件乘以正变量将返回负数

来自分类Dev

将Powershell变量传递到Windows批处理文件

来自分类Dev

将变量从vbs传递到批处理文件

来自分类Dev

批处理文件将wmi输出设置为变量

来自分类Dev

将环境变量传递到批处理文件

来自分类Dev

批处理文件将特定IP转换为变量

来自分类Dev

将命令存储在批处理文件中的变量中

Related 相关文章

  1. 1

    将批处理文件添加到PATH

  2. 2

    批处理文件以将Java密码学扩展文件添加到更新的Java

  3. 3

    使用批处理文件中的msqldump将时间戳记添加到文件名

  4. 4

    批处理文件以将行添加到FTP上的CSV文件

  5. 5

    批处理文件 - 将文件名添加到 txt 的第一行

  6. 6

    将分钟(整数)及时添加到批处理文件中

  7. 7

    如何将文件夹名称添加到批处理文件中正在创建的文件中?

  8. 8

    创建批处理文件,以文件名中的某些字符串将文件添加到数组

  9. 9

    使用批处理文件将父目录名称作为前缀添加到子目录文件

  10. 10

    批处理文件,将日期和时间添加到输出txt文件的每一行的开头

  11. 11

    使用批处理文件将文本添加到文件名末尾(但在扩展名之前)

  12. 12

    如何使用批处理文件将文本添加到多个文本文件的开头?

  13. 13

    如何将批处理文件添加到Visual Basic中并按一个按钮执行?

  14. 14

    批处理文件-将计算的日期添加到文件末尾

  15. 15

    批处理文件-将计算的日期添加到文件末尾

  16. 16

    批处理文件-循环以将字符串追加到变量

  17. 17

    批处理文件可将数字前缀添加到基于月份字符串的文件名中

  18. 18

    如何在Windows批处理文件中将后缀添加到ffmpeg输出的重复文件名?

  19. 19

    批处理文件将“ =”替换为“?” 在文件或变量中

  20. 20

    在嵌入式XP中将基本日期格式添加到批处理文件中

  21. 21

    为什么Wallpaper.reg在添加到批处理文件时不执行其应做的工作?

  22. 22

    将变量传递到批处理文件中

  23. 23

    批处理文件乘以正变量将返回负数

  24. 24

    将Powershell变量传递到Windows批处理文件

  25. 25

    将变量从vbs传递到批处理文件

  26. 26

    批处理文件将wmi输出设置为变量

  27. 27

    将环境变量传递到批处理文件

  28. 28

    批处理文件将特定IP转换为变量

  29. 29

    将命令存储在批处理文件中的变量中

热门标签

归档