JNA-based operating system and hardware information library for Java providing cross-platform access to system metrics
npx @tessl/cli install tessl/maven-com-github-oshi--oshi-core@6.8.0OSHI is a comprehensive Java library that provides cross-platform access to operating system and hardware information without requiring additional native library installations. Built on JNA (Java Native Access), it enables developers to retrieve detailed system metrics including OS version, process information, memory and CPU usage, disk and partition data, hardware devices, and sensor readings across Windows, macOS, and Linux platforms.
<dependency><groupId>com.github.oshi</groupId><artifactId>oshi-core</artifactId><version>6.8.0</version></dependency>import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;For specific components:
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
import oshi.software.os.OSProcess;
import oshi.software.os.FileSystem;import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;
// Create SystemInfo instance - main entry point
SystemInfo si = new SystemInfo();
// Get hardware information
HardwareAbstractionLayer hal = si.getHardware();
long totalMemory = hal.getMemory().getTotal();
String cpuName = hal.getProcessor().getProcessorIdentifier().getName();
// Get operating system information
OperatingSystem os = si.getOperatingSystem();
String osFamily = os.getFamily();
int processCount = os.getProcessCount();
System.out.println("OS: " + osFamily);
System.out.println("CPU: " + cpuName);
System.out.println("Memory: " + totalMemory / 1024 / 1024 + " MB");
System.out.println("Processes: " + processCount);OSHI is built around several key components:
Core entry point and platform detection for accessing system information across different operating systems.
class SystemInfo {
SystemInfo();
OperatingSystem getOperatingSystem();
HardwareAbstractionLayer getHardware();
static PlatformEnum getCurrentPlatform();
}
enum PlatformEnum {
MACOS, LINUX, WINDOWS, SOLARIS, FREEBSD, OPENBSD, WINDOWSCE, AIX, ANDROID, GNU, KFREEBSD, NETBSD, UNKNOWN;
String getName();
static PlatformEnum getValue(int osType);
}Comprehensive hardware component access including CPU, memory, storage, network interfaces, and sensors with detailed performance metrics.
interface HardwareAbstractionLayer {
CentralProcessor getProcessor();
GlobalMemory getMemory();
List<HWDiskStore> getDiskStores();
List<NetworkIF> getNetworkIFs();
List<PowerSource> getPowerSources();
ComputerSystem getComputerSystem();
List<Display> getDisplays();
Sensors getSensors();
List<UsbDevice> getUsbDevices(boolean tree);
List<SoundCard> getSoundCards();
List<GraphicsCard> getGraphicsCards();
}Operating system details, process management, file system access, and network configuration with comprehensive process monitoring capabilities.
interface OperatingSystem {
String getFamily();
String getManufacturer();
OSVersionInfo getVersionInfo();
FileSystem getFileSystem();
List<OSProcess> getProcesses();
OSProcess getProcess(int pid);
int getProcessCount();
int getBitness();
long getSystemUptime();
NetworkParams getNetworkParams();
List<OSSession> getSessions();
List<OSService> getServices();
}Detailed process and thread information including CPU usage, memory consumption, and process relationships with filtering and sorting capabilities.
interface OSProcess {
String getName();
String getPath();
String getCommandLine();
int getProcessID();
int getParentProcessID();
long getVirtualSize();
long getResidentSetSize();
double getProcessCpuLoadCumulative();
List<OSThread> getThreadDetails();
State getState();
}File system information, mount points, disk usage statistics, and file store details with comprehensive storage metrics.
interface FileSystem {
List<OSFileStore> getFileStores();
long getOpenFileDescriptors();
long getMaxFileDescriptors();
}
interface OSFileStore {
String getName();
String getType();
long getTotalSpace();
long getFreeSpace();
long getUsableSpace();
String getMount();
}Network interface details, IP configuration, and network statistics including bandwidth usage and connection information.
interface NetworkIF {
String getName();
String getDisplayName();
String getMacaddr();
String[] getIPv4addr();
String[] getIPv6addr();
long getBytesRecv();
long getBytesSent();
long getSpeed();
}class SystemInfo {
// Main entry point - no-arg constructor
}
enum PlatformEnum {
MACOS("macOS"),
LINUX("Linux"),
WINDOWS("Windows"),
SOLARIS("Solaris"),
FREEBSD("FreeBSD"),
OPENBSD("OpenBSD"),
AIX("AIX"),
ANDROID("Android"),
UNKNOWN("Unknown");
}interface HardwareAbstractionLayer {
// Hardware component access methods
}
interface CentralProcessor {
ProcessorIdentifier getProcessorIdentifier();
long getMaxFreq();
long[] getCurrentFreq();
List<LogicalProcessor> getLogicalProcessors();
int getLogicalProcessorCount();
int getPhysicalProcessorCount();
}
interface GlobalMemory {
long getTotal();
long getAvailable();
long getPageSize();
VirtualMemory getVirtualMemory();
List<PhysicalMemory> getPhysicalMemory();
}
interface ComputerSystem {
String getManufacturer();
String getModel();
String getSerialNumber();
String getHardwareUUID();
Firmware getFirmware();
Baseboard getBaseboard();
}interface OperatingSystem {
// OS information and process management methods
}
class OSVersionInfo {
String getVersion();
String getCodeName();
String getBuildNumber();
}
interface OSProcess {
enum State {
NEW, RUNNING, SLEEPING, WAITING, ZOMBIE, STOPPED, OTHER, INVALID, SUSPENDED
}
}
interface OSThread {
String getName();
int getThreadId();
OSProcess.State getState();
long getStartTime();
int getPriority();
}class ProcessFiltering {
static final Predicate<OSProcess> ALL_PROCESSES;
static final Predicate<OSProcess> VALID_PROCESS;
static final Predicate<OSProcess> NO_PARENT;
static final Predicate<OSProcess> BITNESS_64;
static final Predicate<OSProcess> BITNESS_32;
}
class ProcessSorting {
static final Comparator<OSProcess> NO_SORTING;
static final Comparator<OSProcess> CPU_DESC;
static final Comparator<OSProcess> RSS_DESC;
static final Comparator<OSProcess> UPTIME_ASC;
static final Comparator<OSProcess> UPTIME_DESC;
static final Comparator<OSProcess> PID_ASC;
static final Comparator<OSProcess> PARENTPID_ASC;
static final Comparator<OSProcess> NAME_ASC;
}