1

I am starting a server using Java:

private void startServerButtonMouseClicked(java.awt.event.MouseEvent evt) {                                               
        SwingUtilities.invokeLater(new Runnable() {
            String[] command = {"CMD", "/C", "activator run"};
            public void run() {
                try {
                    ProcessBuilder processbuilder = new ProcessBuilder(command);
                    processbuilder.directory(new File("D:\\temp"));
                    Process process = processbuilder.start();

                    IOThreadHandler thread = new IOThreadHandler(process.getInputStream());
                    thread.start();
                    // TODO : Server name as key
                    serverProcessMap.put("service", process);

                    } catch (InterruptedException ex) {
                        Logger.getLogger(DevUtilsUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (IOException ex) {
                    Logger.getLogger(DevUtilsUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }   

Now I am trying to kill the same process by passing Ctrl+d. Ctrl+d equivalent is number 4. But it is not working. Why it is not working? I am pasting the Java code for stopping the process:

 OutputStream os = process.getOutputStream();
                    os.write(4);
                    os.flush();
                    os.close();

I referenced this from the following link: Send Ctrl-C to process open by Java

halfer
  • 19,824
  • 17
  • 99
  • 186
arunan
  • 922
  • 1
  • 17
  • 25
  • 2
    Why don't you try with just `os.close()`. After all, CTRL+D signals than STDIN is closed (which is what happens when you close `os`). Also note that a process does not need to die just because stdin is closed; any process that does not use stdin will probably just ignore that... and your process is "CMD", not "activator" – SJuan76 Jul 23 '15 at 08:45
  • Then how can i kill the process mate? – arunan Jul 23 '15 at 09:09

0 Answers0