如何从 java 应用程序 运行 windows 命令提示符?

How can I run windows command promt from java application?

目前我正在尝试使用 GUI 制作应用程序。此应用程序必须 运行 cmd 使用某些命令。我正在尝试这样做。我想实现动作(当我按下按钮 "search"-命令提示正在打开时)。我尝试使用 ProcessBuilder 来实现目标,但没有结果。正如您在下面看到的,我尝试使用 "dir" 命令 运行 cmd。我尝试不使用 运行ning 一些命令,但 CMD 是窃取而不是 运行ning。结果我们在任务管理器中看不到 window 和进程。 这是我的代码:

    package sys.tool;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//Создаем поля

public class MainWindow extends JFrame{
    private JLabel lcn = new JLabel("Enter computer name:");
    private JTextField icn = new JTextField("", 5);
    private JButton search = new JButton("Search");
    private JLabel lun = new JLabel("Enter user name:");
    private  JTextField iun = new JTextField("", 5);
    private JLabel empty = new JLabel("");


public MainWindow (){
    super("SysAdminTool");
    this.setBounds(100, 100, 700 , 90);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setResizable(false);

    Container container = this.getContentPane();
    container.setLayout(new GridLayout(3, 2 , 1, 1));
    container.add(lcn);
    container.add(icn);

    container.add(lun);
    container.add(iun);

    container.add(empty);
    search.addActionListener(new SearchEventListener());
    container.add(search); //this is button
}

class  SearchEventListener implements ActionListener{
    public void actionPerformed (ActionEvent e){
       ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "dir C:\Users"); \here I try to run cmd with command "dir"
    }
}


}

到 OS 上的 运行 命令。

String command = "command of the operating system";
Process process = Runtime.getRuntime().exec(command);

此代码将 dir 命令的结果输出到控制台:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Commander {

    private static final String CHAR_SET_NAME_WINDOWS = "windows-1251";
    private static final String CHAR_SET_UTF = "utf8";
    private static final String CHAR_SET_CP = "cp866";

    private static final String COMMAND = "cmd.exe /c dir";

    public static void main(String[] args) {
        runCommand(COMMAND, CHAR_SET_NAME_WINDOWS);
    }

    private static void runCommand(String command, String charsetName) {

        try {
            Process process = Runtime.getRuntime().exec(command);

            // Get input streams
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), charsetName));
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), charsetName));

            // Read command standard output
            String s;
            System.out.println("Standard output: ");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }

            // Read command errors
            System.out.println("Standard error: ");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

希望对您有所帮助。