简体   繁体   中英

how to close cmd prompt from java

I am making a CLI. There are multiple options to choose from each option executes a 'runnable-jar' file. Everything is working as expected. There is a option to 'Exit Programme'. The problem is when this Exit Programme option is chosen the programme stops as expected but the cmd window is not closed inspite of putting the "exit". Target environment is Windows Server 2012 with java 1.7

Please help how to achieve this?

Below is a sample rundown of the programme

    J:\Generated>java -jar MainMenu.jar

    --------------------------------------

    ConnectAnalyzer
    --------------------------------------
    Enter option:
    1. Send Email
    2. sss
    3. Exit Programme
    []--cursor is here 

This is what happens when pressing 3

J:\Generated>exit
Return code = 1

J:\Generated>[]--cursor is here 

Below is source of MainMenu.java. This line works

 stdin.println("cmd.exe /c start exit");

as expected. Opens a cmd prompt and closes it in a split second.

    package com;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    class SystemProcess implements Runnable
    {
    public SystemProcess(InputStream istrm, OutputStream ostrm) {
          istrm_ = istrm;
          ostrm_ = ostrm;
      }
      public void run() {
          try
          {
              final byte[] buffer = new byte[1024];
              for (int length = 0; (length = istrm_.read(buffer)) != -1; )
              {
                  ostrm_.write(buffer, 0, length);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
      private final OutputStream ostrm_;
      private final InputStream istrm_;
    }


    public class MainMenu {

    public static void main(String[] args) throws Exception
    {

                   boolean status = true;
            while(status){
                  System.out.println("");
                  System.out.println("---------------------------------------------------------------------------");
                  System.out.println("");

                  System.out.println("ConnectAnalyzer");

                  System.out.println("---------------------------------------------------------------------------");
                  System.out.println("Enter option: ");
                  System.out.println("1. Send Email");
                  System.out.println("2. sss");
                  System.out.println("3. Exit Programme");

                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

                    String option = null;
                    option = br.readLine();
                    int i = Integer.parseInt(option);
                    switch(i){

                    case 1: {
                          Runtime rt = Runtime.getRuntime();
                          rt.exec("cmd.exe /c start exec.bat");
                            }
                            ;break;
                    case 2:System.out.println(i+"SUB");
                            break;
                    case 3:{
                         String[] command =
                            {   
                                "cmd",
                            };
                            Process p = Runtime.getRuntime().exec(command);
                            new Thread(new SystemProcess(p.getErrorStream(), System.err)).start();
                            new Thread(new SystemProcess(p.getInputStream(), System.out)).start();
                            PrintWriter stdin = new PrintWriter(p.getOutputStream());
 // stdin.println("put whatever command you wish here");                        
//    stdin.println("dir J:\\ /A /Q");
                            stdin.println("cmd.exe /c start exit");
                        //    stdin.println("java -jar J:/Generated/Testimg.jar");

                            stdin.println("exit");

                            stdin.close();

                       int returnCode = p.waitFor();
                           System.out.println("Return code = " + returnCode);
                        System.exit(1);
                    }
                    default: System.out.println("Invalid Option. Try again");
                            break;
            }



            }
    }
    }

I tried

p.destroy();

this does not closes the cmd prompt.

Only option I can see is using taskkill /PID <int_pid> but i don't know how to get the pid.

I think that executing of DOS command exit from your java code should work. This is relevant however only if your user starts your java program directly from previously opened command prompt window.

I think that clearer solution is to solve this at batch file level. The batch file should run your java program and when it terminates (probably with pre-defined exit code - eg -1) executes exit .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM