1

I create a directory in a Java file and delete it when done.

If I quit during Java work, of course it will not perform the deletion.

I've used addShutdownHook() but it seems to work fine with a shutdown command.

My forced shutdown means when i closing the running batch file window. (:when push X button)

Is there a way to solve it in Java source?

It is my tried source

.
.
.
Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    FileUtils.deleteQuietly(new File(basePath));
                }
            });
.
.
.
윤현구
  • 467
  • 7
  • 19
  • 1
    Please see https://stackoverflow.com/questions/12404712/java-shutdown-hook-not-run?rq=1 and take a look at the answer of Nilesh Jadav. Shutdown Hooks are not guaranteed to be executed. – Korashen Sep 18 '18 at 08:45
  • It sounds like you want to avoid the cmd window when you run java. https://stackoverflow.com/questions/15605467/start-apache-in-background-with-batch-file When you ask the os to kill a process, it does it with varying degrees of severity. If the window isn't present, then it is easier to not close the cmd prompt window. – matt Sep 18 '18 at 09:06

1 Answers1

0

I can not think of a pure java solution. The problem is, if your program is forced to shutdown, e.g. with kill -9 (SIGKILL), there is no chance for your program to hook in between. It will shutdown immediately.

You could use monitoring tools like monit or deamontools or supervisord to perform clean-up operations as soon as your program has been shutdown.

You can also provide a handmade solution like suggested here: Shell Script For Process Monitoring , e.g. using tools like watch.

jschnasse
  • 8,526
  • 6
  • 32
  • 72