0

I have a javaagent which collects certain info while the app is running. It also registers a shutdown hook which at the time of the app exit compares the collected data with some golden data and is expected to exit with a non zero exit status if the comparison fails.

However, I am not able to do this since calling System.exit(exitStatus) seems to force the app into deadlock.

Is there a way to exit with a non-zero exit status FROM WITHIN THE SHUTDOWN HOOK

I also tried using java.lang.Runtime.halt(exitStatus) but it doesn't seem to help. echo $? seems to always indicate the exit status to be 0

My application code

public class Sample {
    public static void main(String args[]) {
        System.out.println("My code");
    }
}

My Agent code:

import java.lang.instrument.Instrumentation;
class CompareDataAtTheTimeOfExit extends Thread {
    @Override
    public void run() {
        // If comparison fails exit with 5
    }
}
class Agent {
    public static void premain(String args, Instrumentation inst) throws Exception {
        Runtime r = Runtime.getRuntime();
        r.addShutdownHook(new CompareDataAtTheTimeOfExit());
    }
}
  • [In this very similar question](https://stackoverflow.com/questions/42127345/how-to-get-return-code-in-shutdown-hook/42128017) someone said it is not possible to chance return code inside of shutdown hook. – Amongalen Apr 08 '19 at 09:24
  • 1
    Possible duplicate of [How to get return code in shutdown hook](https://stackoverflow.com/questions/42127345/how-to-get-return-code-in-shutdown-hook) – Würgspaß Apr 08 '19 at 09:46

0 Answers0