使用java的控制台命令

时间:2014-03-08 21:48:56

标签: java swing

如何向已经运行的应用程序发送命令,而无需更改代码和&用命令重新运行它

    public class Console extends JFrame {

private JButton button;
private JTextArea area;
private JTextField text;

private Thread thread;
private Process process;
private BufferedReader bufferedReader;

private String path = "C:/file/";
private String jar = "file.jar";

public Console() {
    setLayout(new BorderLayout());
    setTitle("Custom Console");

    button = new JButton("Start");
    area = new JTextArea();
    text = new JTextField();

    add(button, BorderLayout.NORTH);
    add(new JScrollPane(area), BorderLayout.CENTER);
    add(text, BorderLayout.SOUTH);
    actions();
}

private void actions() {
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            runJar(path + jar);
        }
    });

    text.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                execCmd(path + jar + " " + text.getText());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });

    this.addWindowListener(new WindowListener() {
        public void windowActivated(WindowEvent arg0) {
        }

        public void windowClosed(WindowEvent arg0) {
            print("Closed");
        }

        public void windowClosing(WindowEvent arg0) {
            print("Closing...");
            process.destroy();

            System.exit(1);
        }

        public void windowDeactivated(WindowEvent arg0) {
        }

        public void windowDeiconified(WindowEvent arg0) {
        }

        public void windowIconified(WindowEvent arg0) {
        }

        public void windowOpened(WindowEvent arg0) {
        }
    });
}

protected void runJar(final String string) {

    thread = new Thread(new Runnable() {
        public void run() {
            try {
                print("Starting...");
                print(path + jar);
                print(string);
                process = Runtime.getRuntime().exec("javaw -jar " + path + jar);
                print("runtime...");
                bufferedReader = new BufferedReader(new     InputStreamReader(process.getInputStream()));
                print("bufferedReader...");

                String input = "";
                print("String input...");
                while ((input = bufferedReader.readLine()) != null) {
                    System.out.println(input);
                    print(input);
                }

                print("finished loop");

                process.waitFor();
                process.destroy();
                print("process destroyed");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

private void print(String text) {
    area.setText(area.getText() + text + "\n");
}

public static void main(String[] args) {
    Console frame = new Console();
    frame.setSize(550, 550);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}

解释上面的代码,我希望我的程序启动并等待进一步的命令继续。我希望能够将这些命令发送到程序中。

任何指针都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您正在使用process.getInputStream()从其他Java进程接收数据。您可以尝试使用类似的方法,通过使用process.getOutputStream()

在另一个方向进行通信

只需编写要发送到该流的命令,并在其他java程序中使用System.in.read()(或System.in的BufferedReader)。