• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Laptop battery level indicator in Java

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!!
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might find it useful..

https://coderanch.com/t/383545/java/java/Laptop-Battery

Thanks
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've actually done this myself in Windows. All you need is JNI with one system call - GetSystemPowerStatus.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic