views:

589

answers:

3

I would like to make a java app that shows the current battery level of my mac OS X.

I have read http://stackoverflow.com/questions/25552/using-java-to-get-os-level-system-information and was wondering how I could modify this to make it display the current % of battery

Thanks

+1  A: 

You'll need to use the Mac OS X APIs:

You can get information about power sources and UPS (uninterruptible power supply) devices using the I/O Kit’s power-source API located in /System/Library/Frameworks/IOKit.framework/Headers/ps. The header files in this folder, IOPowerSources.h and IOPSKeys.h, contain methods and keys to extract information about both external and internal power sources. For example, an application can get a list of attached power sources, request notifications for changes in its power sources, and determine how much power is left in a battery.

[edit]

You will need to use JNI to access the IOKit framework.

The question asks about java, so is there a java interface to these API's ?
Milhous
A: 

Without some OS X equivalent to /proc on linux, I doubt you'll get this done without a System.exec or a JNI call.

Jherico
A: 

I think you have two options.

One is to use JNI to invoke native code to get the battery level.

The other is to invoke the application pmset using System.exec in java and parse the output. I think the arguments to retrieve the battery level is pmset -g ps but you better check the man page

jassuncao