0

I would like to build something like the apache xampp control panel. I would like to create my own control panel that start/stops my server.

I am using Eclipse GUI Builder and I tried to search online for help regarding this but couldn't find anything. Can anyone provide some help to me?

For example, usually when I start my server I would go to command prompt, go to the directory and type run.bat. When stopping the server, I would have to do ctrl + c.

How do I achieve this by clicking a JButton (Start server) and another JButton (Stop server)?

There is nothing much on my coding since it is default Eclipse Swing GUI generated codes.

Nicky
  • 63
  • 1
  • 2
  • 11
  • possible solution : http://stackoverflow.com/questions/8496494/running-command-line-in-java – Pankaj Verma May 19 '16 at 08:44
  • ProcessBulder would be the preferred solution, any other is asking for a lot of boil plating and potential issues – MadProgrammer May 19 '16 at 09:06
  • So if I have a dynamic directory for run.bat, can I make a jtextfield for user to enter the directory where run.bat exist and then listen to the textfield? After which running run.bat from that text field directory after clicking the button? – Nicky May 19 '16 at 09:13

2 Answers2

4

It depends on your OS for the command int the runtime.exec method but in a normal case you can try this:

    JButton startServer = new JButton();
    startServer.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Runtime runtime = Runtime.getRuntime();
            try {
                // Here exec your bat file
                runtime.exec("Path_To_Your_Bat_File");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });

    JButton stopServer = new JButton();
    stopServer.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Runtime runtime = Runtime.getRuntime();
            try {
                //Here get your process id and kill it
                runtime.exec("Get_Process & Kill");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
Jérèm Le Blond
  • 645
  • 5
  • 23
2
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("COMMAND_HERE");

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

Pankaj Verma
  • 191
  • 10