I have a Java application that I need to hide from Dock and I also need to be able to interrupt. This is how I handle shutdown:
import com.apple.eawt.AppEvent.QuitEvent; import com.apple.eawt.QuitHandler; import com.apple.eawt.QuitResponse; import com.apple.eawt.Application; public class MacOSXCustomizer { public void init() { Application application = Application.getApplication(); application.setQuitHandler(new QuitHandler() { public void handleQuitRequestWith(QuitEvent qe, QuitResponse qr) { if(Main.prepareForExit()) { qr.performQuit(); } else { qr.cancelQuit(); } } }); } }
I use my own launcher for Java and I use and Application bundle with my own Java launcher binary. I set LSUIElement to YES which solved my problem with a Dock icon, but then shutdown hook stopped working. My method is called during shutdown (I can log it) but application is terminated even if qr.cancelQuit() is called. It seems like a system is not waiting for response. Even if there is running some operations (2 seconds long) it is not finished. It causes data loss.
I tried to set LSUIElement back to NO and then system cancels shutdown when qr.cancelQuit() is called. No other change was done.
I also tried to create simple Cocoa application which implements only one method:
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { return NSTerminateCancel; }
it displays dialog "The application ShutdownTest canceled logout." but user session is logged out anyway.
My question is how can I solve to not have icon in Dock and Menu bar and to be able to cancel/interrupt shutdown sequence?
Edit: It is not relevant whether it is a Java application. Native applications have same behavior.