1

I am currently working on creating a nifty utility that will beep once the laptop's battery is at 55% or at whatever percent the user wants to get notified.

I was told that Java does not provide any access to this information and for that I will need to use JNA(Java Native Access) by Todd Fast.

I got the jna.jar file downloaded but I don't know what to do next to create that software.
I was helped by a community member who created a code that does just that and it is here:
How to get the remaining battery life in a Windows system?
In the code he leaves a comment Fill The Structure. This is where I get confused. The fill will have to be a class that implements the interface that was mentioned ?
and how do I drop jna.jar from classpath ? (and I don't even know what that means).
I am using eclipse. I think first I will have to include the jar file as an import to the project. Guidance please.

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

2

In the code he leaves a comment Fill The Structure. This is where I get confused. The fill will have to be a class that implements the interface that was mentioned ?

No, the code is ready as-is. The javadoc comment merely describes method's own sole job.


and how do I drop jna.jar from classpath ? (and I don't even know what that means).

The classpath is a collection of disk file system paths to folders containing .class files and/or disk file system paths to individual JAR files containing .class files where Java has to lookup when it needs to load a class for compile or runtime.

Where and how exactly to configure the classpath in turn depends on how you're executing the Java application. E.g. in command console, in an IDE, as a web applciation, etc.


I am using eclipse. I think first I will have to include the jar file as an import to the project

If it's a "plain vanilla" Java applicaiton project and you're executing a main() method in Eclipse by (Ctrl)F11, then you need to drop the JAR file just straight in the project root folder, rightclick it and choose Build Path > Add to Build Path. That's it. You can manage them all in Java Build Path entry of project's properties.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Working on it sir, would you like to test / review my application when it is ready ? No, it is not plain-vanilla, it is GUI-based :) – An SO User Jan 15 '13 at 15:54
  • Done and it is working sir and I have a question. As a student I can only learn stuff for which detailed tutorials are available. I don't know how to learn these new APIs and stuff if no tuts are available for them. How do you veterans do that ? – An SO User Jan 15 '13 at 16:01
  • Also, I have reached the limits for asking my questions for 30 days. So here is the last one : how do I selectively retrieve the battery percentage from the code you gave ? – An SO User Jan 15 '13 at 16:04
  • 1
    Read a lot and learn how it works "under the covers". Java is platform independent. Getting battery status is platform dependent. So it'd make sense that Java doesn't offer builtin magic for this. JNI is mentioned in basic Java tutorials as the solution for platform dependent logic. From there, it's only one step to JNA which reduces a lot of JNI boilerplate code so that you end up with much simpler code. – BalusC Jan 15 '13 at 16:06
  • 1
    As to your new question, just invoke the desired `getXxx()` method on `batteryStatus`. That part is in turn just basic Java. Nothing special. – BalusC Jan 15 '13 at 16:07
  • Get ready to put on your tester's hat for some black box, white box testing. I am hot on the job for creating the GUI :) – An SO User Jan 15 '13 at 16:07