Java Apps on OS X, Part I: Sleep Events
I have been working on a few improvements to our product at work. One of the improvements I have been looking to get in is the ability to have the software to detect when the system is going to sleep, and when it is waking up. A quick Google search will turn up several pages that discuss the Notifications that are posted via NSWorkspace’s Notification Center when those events occur.
But I have been having issues getting it to work. I would setup my application to register as an observer of those events, but none of my tests would cause those events to be generated. I wrote a Cocoa-Java app in Xcode that did the same thing, and it work perfectly, with notifications being posted both before and after the sleep event. Yet it wasn’t working in my Java code.
After much searching and playing, I finally found the cause. The product is currently a pure Java application bundled up inside an OS X application bundle for ease of deployment. It turns out that the system sleep and wake events do not get posted if your application is a Pure Java app, but they do get posted if you have a Cocoa-Java app.
So, after much fighting with Xcode I was able to craft a Cocoa-Java version of the application that had a single object in it that simply instantiated the main class of the Java application. Once this was built and run, the application immediately started getting the sleep and wake notifications.
So, for all you Java developers looking to run your software on OS X, be aware of little gotchas like this.
Related Posts:
- Java Apps on OS X, Part II: Loading Libraries
- iPhone Apps
- Application Design Theory
- Notification Based Design
- How Do You Lose Track of a Nuclear Weapon?




Leave a Comment