0

I'm trying execute an taskkill /F /IM in a .exe that is running before close my java application. But I have differents results between stop and exit my java app. When I stop application, I can see my .exe being closed. But when I try exit the java application (for exemple, executing the java -jar Main in command line), my .exe does not is closed. See my code:

public class Main extends Thread {
    public static void main(String[] args) {
        Main main = new Main();
        main.start();
    }

    @Override
    public synchronized void start() {
        super.start();
        // The task done during execution is here.
    }

    @Override
    public void interrupt() {
        super.interrupt();
        try {
            String address = configurator.getValue(FILE_PATH);
            Runtime.getRuntime().exec("taskkill /F /IM " + new String[]{address + APP_NAME});
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

So, what is the difference bettwen stop and exit a java application?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What do you mean by "stop" and "exit"? I assume you're talking about the `stop()` and `exit()` methods for a Java [Thread](https://docs.oracle.com/javase/9/docs/api/java/lang/Thread.html), but I don't see you using those methods anywhere in your code. – Francis Bartkowiak Jan 31 '18 at 13:21
  • In fact, the java application runs as a service through the wrapper by monitoring an .exe, and I have to ensure that before stopping this service, my .exe will also shut down. I use Intellij for testing. When I press your stop button, my goal is reached, but when trying to close my java app without IDE support, it's like it's not the same thing. – Fabian Brandão Jan 31 '18 at 13:28
  • stop = via intellij and exit = through the CMD or service interruption – Fabian Brandão Jan 31 '18 at 13:29
  • Ok. Thank you for the clarification. I think [this post](https://stackoverflow.com/a/2670959/8972283) might be of interest to you. You might find [this article](https://examples.javacodegeeks.com/desktop-java/swing/close-application-properly/) helpful as well. – Francis Bartkowiak Jan 31 '18 at 13:34
  • System.exit() will give me control over when to terminate the application. My problem is to terminate an .exe when the application in java is not paralyzed of course. I wanted a way to listen to the shutdown of the application and do something before it was finalized. – Fabian Brandão Jan 31 '18 at 15:41
  • Because the java application works in a while (true), the application's closure is forced. – Fabian Brandão Jan 31 '18 at 15:52
  • 1
    https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread- – Holger Jan 31 '18 at 17:12
  • Holger, this solve my probleam. Thank you! – Fabian Brandão Jan 31 '18 at 20:02

0 Answers0