1

I'm working with Java Agent (creating a profiler) using code instrumentation (using Javassist for Instrumentation). I need to run few functions in my Java Agent profiler after the complete execution of java program. Something after the main function, like post-main (like we have premain). Is that possible?

ammar26
  • 1,584
  • 4
  • 21
  • 41

1 Answers1

3

There is no such thing as a postmain method and its semantics would not be clear either. Many programs run until they are killed. This requires the application to terminate and not to run different code.

Java offers shutdown hooks via the Runtime class that are triggered on an application's termination but which must not perform long-lasting operations. Also, they are not executed if a program is killed.

For a profiler, you would need to process data regularly and you could attempt to flush your buffers on termination without a guarantee.

Holger
  • 285,553
  • 42
  • 434
  • 765
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192