简体   繁体   中英

Java Open external program then close java program

Alright, I have a shutdown hook that makes sure I close my connection nicely whenever I close. What I need to do now is open another program (terminal emulator) then close mine while leaving the terminal emulator open. I am able to open the emulator but the java program doesn't close until the emulator closes. How can I run something and close my program out?

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            if (connection != null) {
                try {
                    connection.close();
                    System.out.println("Connection Closed");
                    try {
                        ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "C:\\MVbase\\mvterm.lnk");
                        Process p = pb.start();
                        System.exit(0);
                    } catch (IOException e) {
                        System.out.println(e);
                    }
                } catch (MVException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }, "Shutdown-thread"));

The easiest way to do it is to make a shell script or batch file that runs the program then shuts it down and starts the terminal emulator.

#!/bin/sh
cd C:/location/of/program 
javac program.java
java program
kill program
cd C:/location/of/terminalemulator
run terminalemulator

Save this as a batch file and it should do it all from the command line.

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