B4J Question Is it possible to read system information (CPU, RAM, temperature) in B4J without external tools?

Mania

Member
Licensed User
Longtime User
Hello!
I am working on a B4J application and I would like to read basic system information from the local computer, such as:
  • CPU usage
  • RAM usage (total / free)
  • System temperature (CPU or motherboard, if available)
My question is: is it possible to obtain such data directly in B4J, for example using Java APIs, without relying on any external programs, command-line tools, or third-party native applications?
I am aware that B4J is based on Java, so I am mainly interested in:
  • What information can be accessed via standard Java libraries?
  • Whether temperature sensors are accessible at all without OS-specific external utilities?
The target platform is Windows11 pro (but cross-platform solutions would be a plus).
Any guidance, examples, or limitations I should be aware of would be greatly appreciated.


Thank you in advance.
 

behnam_tr

Active Member
Licensed User
Longtime User

B4X:
'Hardware information
'Windows Only
'system information

    Dim p As jUUID
    Log("OS_Name : "&p.OS_Name)
    Log("OS_Version: "&p.OS_Version)
    Log("OS_Arch : "&p.OS_Arch)
    Log("OS_username : "&p.OS_UserName)
    Log("OS_SerialNumber : "&p.OS_SerialNumber)
    Log("MainBoard Name : "&p.Get_MotherBoardName)
    Log("CPU Name : "&p.Get_CPUName)
    Log("CPU Cores : "&p.Get_CPUCores)
    Log("CPU LogicalProcessors : "&p.Get_CPULogicalProcessors)
    Log("Total PhysicalMemory : "&Round(p.Get_TotalPhysicalMemory/1024/1024)&" MB")
    Log("Free PhysicalMemory : "&Round(p.Get_AvailablePhysicalMemory/1024)&" MB")
    Log("GPU Name: "&p.Get_GpuName)
    Log("Bios Version : "&p.Get_BiosVersion)
 
Upvote 1

behnam_tr

Active Member
Licensed User
Longtime User
 
Upvote 1

Mania

Member
Licensed User
Longtime User
Thank you for sharing the library.

Is there any way to read CPU temperature in B4J using this approach, without external tools or native executables?
Or is CPU temperature not accessible due to OS / Java limitations?

Thanks in advance.
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
This Java library looks promising:


especially if it is truly as easy to use as:

https://github.com/profesorfalken/jSensors#:~:text=Basic Usage,CPUs, GPUs, Disks...

1767765278666.png
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
#AdditionalJar: C:\Users\H\Downloads\jna-5.2.0.jar
#AdditionalJar: C:\Users\H\Downloads\jPowerShell-3.1.1.jar
#AdditionalJar: C:\Users\H\Downloads\jSensors-2.2.1.jar
#AdditionalJar: slf4j-api-1.7.30
#AdditionalJar: slf4j-jdk14-1.7.35
Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    Test
    Log("done")
End Sub

Private Sub Test
    Dim JSensors As JavaObject
    JSensors = JSensors.InitializeStatic("com.profesorfalken.jsensors.JSensors").GetField("get")
    Log("getting cpus...")
    Dim components As JavaObject = JSensors.RunMethodJO("components", Null)
    Dim cpus As List = components.GetField("cpus")
    For Each cpu As JavaObject In cpus
        Log("cpu: " & cpu.GetField("name"))
        Dim sensors As JavaObject =  cpu.GetFieldJO("sensors")
        If sensors.IsInitialized Then
            Log("sensors: ")
            Dim temps As List = sensors.GetField("temperatures")
            For Each temp As JavaObject In temps
                Log(temp.GetField("name") & ": " & temp.GetField("value") & " C")
            Next
            For Each fan In sensors.GetField("fans").As(List)
                Log(fan)
            Next
        End If
    Next
End Sub

Looks like only the cpu name actually works. At least on my PC.
 
Upvote 0

Mania

Member
Licensed User
Longtime User
Thank you for the clarification.


I observed a similar behavior when testing CPU temperature access with Python using LibreHardwareMonitor.
When the script was run without administrator privileges, only basic information (such as CPU name) was available.
After running it as Administrator, CPU temperature readings became available and worked correctly.


So it seems that, at least on Windows, access to CPU temperature depends on elevated privileges, not on the language or library itself.
 
Upvote 0
Top