I am developing an application which monitors the presence of the power supply of the laptop. If there is a power cut or restoration it will intimate me over email. It will also application monitoring and controlling over email (Basically to control my laptop from my office over email). I am done with email interfacing but I have no idea on how to monitor the power supply / battery supply from java.
If any can give some pointer on this it will be of great help.
Thanks in advance ....
A quick and dirty way to handle this is call a native program (via Runtime.exec(...)) and parse the output. On Windows, the native program might be VBScript that uses WMI.
Here's code that works on Windows by using the
SYSTEM_POWER_STATUS
structure.Note that you need to add
jna
to your (Maven) dependencies for this to work.In your code call it like this:
Result:
This has been derived off BalsusC's answer.
A quick google search turns up a java acpi library on sourceforge. Hasn't been updated since 2004 though.
On linux, you can use /proc/acpi/battery/
You have probably already solved this problem but for the others - you can do it the way Adam Crume suggests, using an already written script battstat.bat for Windows XP and higher. Here is an example of the resulting function:
Or you can simplify the script to return directly True/False or whatever fits.