Retrieve computer temperature in Java
Solved
Valentin97
Posted messages
14
Status
Membre
-
scriptiz Posted messages 1494 Status Membre -
scriptiz Posted messages 1494 Status Membre -
Hello,
After doing several searches on Google, I couldn't find the answer to my question: retrieving the temperature of the computer through the sensors. The answers Google provided were mostly focused on C / C++ (I only know the basics of C).
Thank you for your help.
PS: I'm not sure if I'll be able to respond to your answers right away (if they provide a solution in Java) because I don't know if my computer has all its sensors activated since when I type in a terminal:
If my sensors are not working, I will be able to give you a response sometime later when I get a new PC.
Thank you in advance.
Configuration: Linux (Ubuntu 11.04) / Firefox 12.0
After doing several searches on Google, I couldn't find the answer to my question: retrieving the temperature of the computer through the sensors. The answers Google provided were mostly focused on C / C++ (I only know the basics of C).
Thank you for your help.
PS: I'm not sure if I'll be able to respond to your answers right away (if they provide a solution in Java) because I don't know if my computer has all its sensors activated since when I type in a terminal:
sensorsIt always shows the same temperature: 21.8° Celsius.
If my sensors are not working, I will be able to give you a response sometime later when I get a new PC.
Thank you in advance.
Configuration: Linux (Ubuntu 11.04) / Firefox 12.0
3 réponses
You can call DLLs (either Win32 APIs or your own DLLs created in C++) with JNI.
Otherwise, there are other libraries to do this like JNA which they mention here:
https://www.developpez.net/forums/d600497/java/general-java/api-standards-tierces/java-api-windows/
But I don't think Java allows access via its standard library.
"The most successful method of programming is to begin a program as simply as possible, test it, and then add to the program until it performs the required job." -- PDP8 handbook, Pg 9-64
Otherwise, there are other libraries to do this like JNA which they mention here:
https://www.developpez.net/forums/d600497/java/general-java/api-standards-tierces/java-api-windows/
But I don't think Java allows access via its standard library.
"The most successful method of programming is to begin a program as simply as possible, test it, and then add to the program until it performs the required job." -- PDP8 handbook, Pg 9-64
In that case, he can easily call the sensors command from his Java program and extract the result he is interested in (with a split or a regular expression).
// The command to retrieve temperatures String cmd = "sensors"; try { // Launching the sensors command Process child = Runtime.getRuntime().exec(cmd); // Getting the output of the process InputStream lsOut = child.getInputStream(); InputStreamReader r = new InputStreamReader(lsOut); BufferedReader in = new BufferedReader(r); // Displaying the output of the process String line; while ((line = in.readLine()) != null) { System.out.println(line); } } catch (Exception e) { // In case of error System.out.println("Command failed: " + cmd); }I'll also take the opportunity to display the error stream reading, it can be useful.
// The command to retrieve the temperatures String cmd = "sensors"; try { // We launch the sensors command Process child = Runtime.getRuntime().exec(cmd); // We retrieve the standard output of the process Scanner out = new Scanner(child.getInputStream()); // We retrieve the error output of the process Scanner err = new Scanner(child.getErrorStream()); // We display the outputs of the process while (out.hasNextLine()) System.out.println(out); while (err.hasNextLine()) System.err.println(err); // We free the process out.close(); err.close(); child.destroy(); } catch (Exception e) { // In case of an error System.err.println("Command failed: " + cmd); e.printStackTrace(); }