How to get the battery power level in % in Java in a platform independent way?

2.4k views Asked by At

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?

2

There are 2 answers

0
chrylis -cautiouslyoptimistic- On

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.)

0
Mateusz On

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.