aspose file tools*
The moose likes Java in General and the fly likes Laptop battery level indicator in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Laptop battery level indicator in Java" Watch "Laptop battery level indicator in Java" New topic
Author

Laptop battery level indicator in Java

Ashish S Yadav
Ranch Hand

Joined: Apr 08, 2012
Posts: 31
I can already see battery level for a laptop. But I want to see it in a java program. Can we use Java to see the battery level for the laptop? If not, then which language can we use?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
java has automatic garbage collecting and prevents individuals from handling/manipulating memory freely which can be undesirable for those who need total control when handling devices.
For performance reasons, it is unrecommended to use Java in place of C/C++ when it comes to doing low-level systematic commands.
you can use jni for native support in java.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:java has automatic garbage collecting and prevents individuals from handling/manipulating memory freely


This has nothing to do with the OP's question.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2

This has nothing to do with the OP's question.

sorry for misinterpret jeff.
but what i was trying to say is, java is limited up to the jvm, so doing system programming in java is difficult and effects the performance, therefore it's better to use native support in java.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14433
    
  23

Punit Jain wrote:For performance reasons, it is unrecommended to use Java in place of C/C++ when it comes to doing low-level systematic commands.

And the reason that you can't do very platform-specific things in Java does not have anything to do with performance.

Anyway, I don't know of any Java API that allows you to read the battery level of a laptop. There might be a third-party library for this; it's certainly not in the standard Java API. Any third-party library will likely use some native code to read the battery level, since the JVM doesn't have anything for this built-in.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:doing system programming in java is difficult


"Impossible" would be a better term, depending on what you mean by "system programming." And if your definition does include something that Java can do, I wouldn't say it's particularly difficult in Java, unless you can provide a specific example with details.

and effects the performance,


No it doesn't, unless you can provide a specific example with details and numbers.

therefore it's better to use native support in java.


The reason it's better not to use Java for system programming is that Java was not made for system programming.
Palak Mathur
Ranch Hand

Joined: Jan 29, 2007
Posts: 313

Punit Jain wrote:

This has nothing to do with the OP's question.

sorry for misinterpret jeff.
but what i was trying to say is, java is limited up to the jvm, so doing system programming in java is difficult and effects the performance, therefore it's better to use native support in java.


@Punit are you sure about what you are saying? First of all Java cannot do system programming and moreover, since it is impossible to do so, performance issues will not come into picture!!


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2

what you mean by "system programming"

in java "system programming" i mean running dos commands and all, i have only executed dos commands in java, haven't done system programming myself.
i mean by something like this:


By performance i mean that "calling native methods are faster".
i read this here:
here

i apologies if i took it in other sense.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:
i mean by something like this:


By performance i mean that "calling native methods are faster".


Ah, I see.

Yes, it's true that in general it will be faster to call a native method to perform some task, rather than invoking an external process to perform the same task. However, this is also true in C/C++. It's not Java specific at all, and I don't see what it has to do with this thread.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Jeff Verdegan wrote:
Punit Jain wrote:
i mean by something like this:


By performance i mean that "calling native methods are faster".


Ah, I see.

Yes, it's true that in general it will be faster to call a native method to perform some task, rather than invoking an external process to perform the same task. However, this is also true in C/C++. It's not Java specific at all, and I don't see what it has to do with this thread.


Haven't done this before but i think linux has a command acpi
which gives output like this:

Battery 1: discharging, 44%, 00:18:48 remaining

and i think we can run linux commands as well in java in the same manner.
not sure about windows, but it has powercfg but don't know does it has any parameter to display above output.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Punit Jain wrote:Haven't done this before but i think linux has a command acpi
which gives output like this:

Battery 1: discharging, 44%, 00:18:48 remaining

and i think we can run linux commands as well in java in the same manner.
not sure about windows, but it has powercfg but don't know does it has any parameter to display above output.


With Windows, you can use the following command-line command:

c:\Users\xxxxx> typeperf "\Battery Status(*)\Remaining Capacity"

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Henry Wong wrote:
Punit Jain wrote:Haven't done this before but i think linux has a command acpi
which gives output like this:

Battery 1: discharging, 44%, 00:18:48 remaining

and i think we can run linux commands as well in java in the same manner.
not sure about windows, but it has powercfg but don't know does it has any parameter to display above output.


With Windows, you can use the following command-line command:

c:\Users\xxxxx> typeperf "\Battery Status(*)\Remaining Capacity"

Henry


thank you henry
but it's showing error:
Error: No Valid Counters.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 193

You might find it useful..

http://www.coderanch.com/t/383545/java/java/Laptop-Battery

Thanks


Do not wait to strike till the iron is hot; but make it hot by striking....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Punit Jain wrote:thank you henry
but it's showing error:
Error: No Valid Counters.


That means that the counter is not available -- one possible reason is that you don't have a battery in the machine....

FYI.... you can also use the "-qx" flag to list all the available counters.

Henry
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1001
    
    2
Henry Wong wrote:
Punit Jain wrote:thank you henry
but it's showing error:
Error: No Valid Counters.


That means that the counter is not available -- one possible reason is that you don't have a battery in the machine....

FYI.... you can also use the "-qx" flag to list all the available counters.

Henry


-qx showing me a huge list of counter and there is not any counter named "\Battery Status(*)\Remaining Capacity"
this is the counter, right??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

Punit Jain wrote:-qx showing me a huge list of counter and there is not any counter named "\Battery Status(*)\Remaining Capacity"
this is the counter, right??


Yes, except for the (*) part. That part should be replaced with the battery name -- this is for machines with more than one battery.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

I've actually done this myself in Windows. All you need is JNI with one system call - GetSystemPowerStatus.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Punit Jain wrote:in java "system programming" i mean running dos commands and all, i have only executed dos commands in java, haven't done system programming myself.

That is a very unique definition of system programming. Most folks try to use commonly held definitions to help understanding.

For most folks, "system programming" means operating at the device or interrupt level. In most cases, this means programing the operating system itself, not using the operating system. Or perhaps a device driver.

Executing a DOS (sic) command shell program or script is not close to system programming, that is simply user mode application usage.

By definition, nearly all system programming is somewhat hardware and operating system dependent, which is not an area of programming that Java aims at. Java aims for write once, run everywhere, which is impossible for device specific stuff.
Ashish S Yadav
Ranch Hand

Joined: Apr 08, 2012
Posts: 31
Rob Spoor wrote:I've actually done this myself in Windows. All you need is JNI with one system call - GetSystemPowerStatus.


How do i make java do that job?
Can you suggest some sample java code which will invoke the windows commands ? Can i also see if the laptop is connected to AC power or not ?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Ashish S Yadav wrote:Can you suggest some sample java code which will invoke the windows commands ? Can i also see if the laptop is connected to AC power or not ?


You need to read up on JNI, You don't 'invoke the windows command' you simply call a method in a JNI library.
The things you can or can not do is defined by the specific JNI library that you are using.

You need to do your own work and show some effort
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

The Java code is the easy part here. For instance:
The actual code is written in C or C++. That's why I quoted that Windows system call. You write C or C++ code to call that after you include <windows.h>.

As for your second question, the SYSTEM_POWER_STATUS struct that is filled by the GetSystemPowerStatus call includes field ACLineStatus, which will be 0 if the adapter is not connected, 1 if it is, or 255 if it can't be determined. On my desktop machine it returns 1. You can use the BatteryFlag to determine if you actually have an adapter - 128 means it isn't. That's what I get.
Ashish S Yadav
Ranch Hand

Joined: Apr 08, 2012
Posts: 31
Rob Spoor wrote:The Java code is the easy part here. For instance:
The actual code is written in C or C++. That's why I quoted that Windows system call. You write C or C++ code to call that after you include <windows.h>.

As for your second question, the SYSTEM_POWER_STATUS struct that is filled by the GetSystemPowerStatus call includes field ACLineStatus, which will be 0 if the adapter is not connected, 1 if it is, or 255 if it can't be determined. On my desktop machine it returns 1. You can use the BatteryFlag to determine if you actually have an adapter - 128 means it isn't. That's what I get.


I am new to this concept and jni. Is it possible to get some existing C++ program which does the battery checking, then "wrap" java around it and make java get all the info that the C++ code pulls out ?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Ashish S Yadav wrote:I am new to this concept and jni. Is it possible to get some existing C++ program which does the battery checking, then "wrap" java around it and make java get all the info that the C++ code pulls out ?


As folks have answered up thread, yes. You need to do some research on JNI. Here at the ranch, you are expected to show some effort.
Ashish S Yadav
Ranch Hand

Joined: Apr 08, 2012
Posts: 31
Pat Farrell wrote:
Ashish S Yadav wrote:I am new to this concept and jni. Is it possible to get some existing C++ program which does the battery checking, then "wrap" java around it and make java get all the info that the C++ code pulls out ?


As folks have answered up thread, yes. You need to do some research on JNI. Here at the ranch, you are expected to show some effort.


Okay. I will do that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Laptop battery level indicator in Java