Turn your Mobile Apps into m-commerce apps – Learn More!
 

Code Snippets

  

Java Source Code


You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 415,726 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,718 people online right now.Registration is fast and FREE... Join Now!




Java Windows Battery Life

Uses WMI to fetch the battery status. From here it extract the battery life (percent charge).

Submitted By: NickDMax
Actions:
Rating:
Views: 985

Language: Java

Last Modified: March 24, 2009
Instructions: This snippet uses the JACOB COM bridge, to use this snippet you need to install JACOB and put the jacob.jar and appropriate DLL file in you classpath.

Snippet


  1. import com.jacob.activeX.ActiveXComponent;
  2. import com.jacob.com.Dispatch;
  3. import com.jacob.com.EnumVariant;
  4. import com.jacob.com.Variant;
  5.  
  6. /**
  7. * Quick little example of using JACOB (an open source Java-COM bridge) to access
  8. * the windows WMI interface.
  9. * <br>
  10. * To use this snippet you will need to download and install JACOB. This is relatively easy:
  11. * I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
  12. *
  13. * @author NickDMax (at) DreamInCode
  14. */
  15. public class BatteryLife {
  16.  
  17.         /**
  18.         * Determine how much battery life is left (in percent).
  19.         *
  20.         * @param args
  21.         */
  22.         public static void main(String[] args) {
  23.                 String host = "localhost"; //Technically you should be able to connect to other hosts, but it takes setup
  24.                 String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
  25.                 String query = "Select * from Win32_Battery";
  26.                 ActiveXComponent axWMI = new ActiveXComponent(connectStr);
  27.                 //Execute the query
  28.                 Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
  29.                
  30.                 //Our result is a collection, so we need to work though the collection.
  31.                 // (it is odd, but there may be more than one battery... think about multiple
  32.                 //   UPS on the system).
  33.                 EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
  34.                 Dispatch item = null;
  35.                 while (enumVariant.hasMoreElements()) {
  36.                         item = enumVariant.nextElement().toDispatch();
  37.                         int percentLife = Dispatch.call(item,"EstimatedChargeRemaining").getInt();
  38.                         System.out.printf("Battery life remaining: %3d%%\n",percentLife );
  39.                 }
  40.         }
  41. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.