-3

Please know that stanford is not exe. it is a folder consists many programs

I open the cmd.exe by using following statement:

public static void runStanfordCMD() throws IOException{
    List<String> cmds = Arrays.asList("cmd.exe", "/C", "start", "java", "-mx4g", "-cp", "*", "edu.stanford.nlp.pipeline.StanfordCoreNLPServer");
    ProcessBuilder builder = new ProcessBuilder(cmds);
    builder.directory(new File("D:/Desktop/stanford-corenlp-full-2015-12-09"));
    Process proc = builder.start();
}

so how to close the cmd.exe after I finished some process?

by using ProcessBuilder or Runtime? If using ProcessBuilder how to write the statement according to my case?

how to write the statement to the Runtime according to my case?

public static void closeStanfordCMD() throws IOException{
    try {
        Runtime.getRuntime().exec("command.exe  /C" + "Your command");  // how to write the statement?

    } catch (Exception e) {
        e.printStackTrace();  
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
bob90937
  • 553
  • 1
  • 5
  • 18
  • 1
    you asked , so how to close the cmd.exe after I finished some process?? but your title question is insane. which program do you want to close , standford folder or programs under standford folder? – Ye Win Nov 11 '16 at 08:55
  • 2
    Possible duplicate of [Killing a process using Java](http://stackoverflow.com/questions/6356340/killing-a-process-using-java) – ArcticLord Nov 11 '16 at 08:56

2 Answers2

1

// how to write the statement?

if you want to close command prompt:

Runtime.getRuntime().exec("taskkill /IM " + "cmd.exe");

if you want to close stanford.exe:

Runtime.getRuntime().exec("taskkill /IM " + "stanford.exe");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ye Win
  • 2,020
  • 14
  • 21
1

The method Runtime.getRuntime().exec returns an object of Process.

Process has a method destroy() which kills the process you're running.
So what you need is to store the Process object returned by the exec method and call destroy() on it when you want to kill the process.

Also you can call waitFor() method to wait for the process to stop (Causes the current thread to wait, if necessary, until the process represented by this Process object has terminated).

To make it clear, try this code (or modify for your needs):

try {
    Process p = Runtime.getRuntime().exec("java -mx4g -cp * D:/Desktop/stanford-corenlp-full-2015-12-09/edu.stanford.nlp.pipeline.StanfordCoreNLPServer");
    p.waitFor();
}
catch (IOException e) {
    e.printStackTrace();
}
catch (InterruptedException e) {
    e.printStackTrace();
}
  • No, it can't close the program – Ye Win Nov 11 '16 at 08:47
  • Well, you shouldn't run cmd.exe. There is no need for that. Instead you need to run the application directly with the exec method. For example Runtime.getRuntime().exec("myapplication.exe"). – Mihály Lestyán Nov 11 '16 at 08:50
  • standford is not .exe – bob90937 Nov 11 '16 at 08:53
  • Since the exec method runs a process, it doesn't need to be an .exe. It can be anything your OS can run. – Mihály Lestyán Nov 11 '16 at 08:54
  • I think this solution should work, but the OP must not use `cmd /c start` to run his/her program, but just `cmd /c` (because `start` would create another -unnecessary- subprocess). – Little Santi Nov 11 '16 at 14:01
  • There is no cmd /c start command in my answer. Please forget starting processes via cmd as you will loose the platform independent behaviour of java. And this is the main point of exec method. – Mihály Lestyán Nov 11 '16 at 14:20