How to get the battery power level in % in Java in a platform independent way?
There is already a question for Windows for this: How to get the remaining battery life in a Windows system?
And for Mac: Get battery level in Java
But how can I do it in a platform-independent way, so it works on Windows, Mac, Linux, and maybe other platforms too?
There's no "platform-independent" way to read battery levels; that sort of hardware information is extremely OS-specific. The only way to have a "platform-independent" Java interface would be to write a JNI wrapper for each target platform.
(Actually, on Linux it's pretty easy; all this information is exposed as text files under /proc/acpi/battery
, so you wouldn't need any JNI there. I don't think OS X has the same friendly presentation, and I'm quite certain Windows doesn't.)
I do not think there is any way to do this, once I was writing project to get Windows machine HW details like CPU temperature, and I had to use java native interface with specific libraries. Getting this for different Windows versions was not that straight forward so I do not think you can do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With