Linux Battery Monitor

Its funny how sometimes you “stumble” on some things. I have been thinking to myself “wont you dosomething cool to wrap up the year with?” I have quite a number of ideas but I dont seem to be able to summon up strength to start any.

But of recent, I have to re-organise my box, so I decided to just format it(I havent done that in a while). So I copied my home directory and blam! formated the box. I wanted to install a lightweight OS, so I chose a version of Ubuntu that is lightweight, comes with xfce and all that. The only problem is, it doesnt have a graphical battery monitor! After searching online for an xfce battery monitor with no success, I decided to write a shell script that prints to one of my consoles. I did that in no time, and whenever I want to check my battery status, I switch to the terminal. If I dont want to switch, I just run the shell script from the terminal. Works well for a geek, but for a user, it sucks. So I decided to give it a UI.

I have not done java for a while(I have been engrossed in scala and erlang) so I decided to do a little java(plus I just need an excuse to start Netbeans again after a long spell of VIM). So I built the UI, far far less complicated than I thought and yea, it works.

Keep your fingers crossed, you will get the source and rpm first thing 2009.

Merry XMAS and Happy New Year….see you on the other side of 8



  1. Andrey said

    I am looking the net to how to get the battery level in Linux by java.
    I’ve just bought the new battery yesterday and today morning decided to write a little prog. to chack how long it lasts. But while writing the program I thought it will be nice to see discharge distribution too.
    Can you halp me please?


    • I think I can help.
      there are two solutions, I used both in my battery monitor. First I check for acpi command
      run acpi -b from the terminal, you should get an output like this
      Battery 1: charging, 93%, 00:00:14 until charged
      If acpi is installed, from your java application do something like this

      Process proc = null;
      try {
      while (true) {
      String output = "command not found";
      try {
      proc = Runtime.getRuntime().exec("acpi -b");
      InputStream in = proc.getInputStream();
      int ch;
      StringBuffer sb = new StringBuffer();
      while ((ch = != -1) {
      sb.append((char) ch);
      output = sb.toString();
      String components = output.split(",");
      if (!components[0].contains("charged")) {
      } else {
      String pct = components[1].trim();
      percent = Double.parseDouble(pct.substring(0, pct.indexOf("%")));
      } catch(Exception e) {

      this is a part of my own code. If acpi was not found, an exception is thrown and then output will be “command not found” so we now go for the second option.

      run cat /proc/acpi/battery/BAT0/state

      run cat /proc/acpi/battery/BAT0/info

      The output from these two files we are gonna use to write a simple bash script

      rem_capacity=`gawk '/remaining capacity:/ {print $3}' /proc/acpi/battery/BAT0/state`
      total_capacity=`gawk '/design capacity:/ {print $3}' /proc/acpi/battery/BAT0/info`
      percent=`echo " scale=4; $rem_capacity/$total_capacity * 100" | bc`
      charging=`gawk -F: '/charging/ {print $2}' /proc/acpi/battery/BAT0/state`
      if [ $charging = $expected ]
      if [ $1 = "shutdown" ]
      echo "shutting down"
      sudo shutdown -h now
      elif [ $1 = "hibernate" ]
      echo "hibernating"
      sudo /etc/acpi/
      echo $charging
      echo $total_capacity
      echo $rem_capacity
      echo $percent

      if you save this file as battery_terminal then this piece of code should work

      if(output.contains("command not found")) {
      proc = Runtime.getRuntime().exec("battery_terminal get");
      InputStream in = proc.getInputStream();
      StringBuffer sb = new StringBuffer();
      int ch;
      while ((ch = != -1) {
      sb.append((char) ch);
      output = sb.toString();
      lblStatus1.setText("Battery Time Remaining: Unknown");
      String pct = output.split("\n")[3];
      DecimalFormat format = new DecimalFormat("##");
      percent = Double.parseDouble(pct);
      lblPercentage.setText(format.format(percent) + "%");

      This also is a bit of my code. If you want the full source, check it out from gitoriuos

      I hope this helps.

  2. Andrey said

    Thank you!
    I will use the first option. acpi is installed 🙂

  3. cool. lemme c what u come up wit

  4. Divine said

    Hi Guys,
    exists no java api to get battey information?
    It looks like a hack to parse output from another program.

    So it is a good solution if no other way exists.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: