0
# Operating System Information
1
2
Operating system details, process management, file system access, and network configuration with comprehensive process monitoring capabilities.
3
4
## OperatingSystem Interface
5
6
Interface providing access to operating system information, processes, file systems, and network configuration.
7
8
```java { .api }
9
interface OperatingSystem {
10
String getFamily();
11
String getManufacturer();
12
OSVersionInfo getVersionInfo();
13
FileSystem getFileSystem();
14
InternetProtocolStats getInternetProtocolStats();
15
List<OSProcess> getProcesses();
16
List<OSProcess> getProcesses(Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);
17
List<OSProcess> getProcesses(Collection<Integer> pids);
18
OSProcess getProcess(int pid);
19
List<OSProcess> getChildProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);
20
List<OSProcess> getDescendantProcesses(int parentPid, Predicate<OSProcess> filter, Comparator<OSProcess> sort, int limit);
21
int getProcessId();
22
OSProcess getCurrentProcess();
23
int getProcessCount();
24
int getThreadId();
25
OSThread getCurrentThread();
26
int getThreadCount();
27
int getBitness();
28
long getSystemUptime();
29
long getSystemBootTime();
30
boolean isElevated();
31
NetworkParams getNetworkParams();
32
List<OSService> getServices();
33
List<OSSession> getSessions();
34
List<OSDesktopWindow> getDesktopWindows(boolean visibleOnly);
35
List<ApplicationInfo> getInstalledApplications();
36
}
37
```
38
39
## OS Version Information
40
41
Operating system version details.
42
43
```java { .api }
44
class OSVersionInfo {
45
String getVersion();
46
String getCodeName();
47
String getBuildNumber();
48
String toString();
49
}
50
```
51
52
## Process Information
53
54
Information about running processes including CPU usage, memory consumption, and process relationships.
55
56
```java { .api }
57
interface OSProcess {
58
String getName();
59
String getPath();
60
String getCommandLine();
61
String getCurrentWorkingDirectory();
62
String getUser();
63
String getUserID();
64
String getGroup();
65
String getGroupID();
66
State getState();
67
int getProcessID();
68
int getParentProcessID();
69
int getThreadCount();
70
int getPriority();
71
long getVirtualSize();
72
long getResidentSetSize();
73
long getKernelTime();
74
long getUserTime();
75
long getUpTime();
76
long getStartTime();
77
long getBytesRead();
78
long getBytesWritten();
79
long getOpenFiles();
80
int getBitness();
81
long getAffinityMask();
82
double getProcessCpuLoadCumulative();
83
double getProcessCpuLoadBetweenTicks(OSProcess priorSnapshot);
84
String[] getArguments();
85
Map<String, String> getEnvironmentVariables();
86
List<OSThread> getThreadDetails();
87
int getMinorFaults();
88
int getMajorFaults();
89
long getContextSwitches();
90
91
enum State {
92
NEW, RUNNING, SLEEPING, WAITING, ZOMBIE, STOPPED, OTHER, INVALID
93
}
94
}
95
```
96
97
## Process Filtering and Sorting
98
99
Utility classes for filtering and sorting process lists.
100
101
```java { .api }
102
class ProcessFiltering {
103
static final Predicate<OSProcess> ALL_PROCESSES;
104
static final Predicate<OSProcess> VALID_PROCESS;
105
static final Predicate<OSProcess> NO_PARENT;
106
static final Predicate<OSProcess> BITNESS_64;
107
static final Predicate<OSProcess> BITNESS_32;
108
}
109
110
class ProcessSorting {
111
static final Comparator<OSProcess> NO_SORTING;
112
static final Comparator<OSProcess> CPU_DESC;
113
static final Comparator<OSProcess> RSS_DESC;
114
static final Comparator<OSProcess> UPTIME_ASC;
115
static final Comparator<OSProcess> UPTIME_DESC;
116
static final Comparator<OSProcess> PID_ASC;
117
static final Comparator<OSProcess> PARENTPID_ASC;
118
static final Comparator<OSProcess> NAME_ASC;
119
}
120
```
121
122
## Thread Information
123
124
Information about threads within processes.
125
126
```java { .api }
127
interface OSThread {
128
String getName();
129
int getThreadId();
130
OSProcess.State getState();
131
long getStartTime();
132
long getStartMemoryAddress();
133
long getContextSwitches();
134
long getKernelTime();
135
long getUserTime();
136
long getUpTime();
137
int getPriority();
138
}
139
```
140
141
## Network Parameters
142
143
Network configuration information.
144
145
```java { .api }
146
interface NetworkParams {
147
String getHostName();
148
String getDomainName();
149
String[] getDnsServers();
150
String getIpv4DefaultGateway();
151
String getIpv6DefaultGateway();
152
}
153
```
154
155
## Internet Protocol Statistics
156
157
Network protocol statistics and connection information.
158
159
```java { .api }
160
interface InternetProtocolStats {
161
TcpStats getTCPv4Stats();
162
TcpStats getTCPv6Stats();
163
UdpStats getUDPv4Stats();
164
UdpStats getUDPv6Stats();
165
List<IPConnection> getConnections();
166
167
class TcpStats {
168
long getConnectionsEstablished();
169
long getConnectionsActive();
170
long getConnectionsPassive();
171
long getConnectionFailures();
172
long getConnectionsReset();
173
long getSegmentsSent();
174
long getSegmentsReceived();
175
long getSegmentsRetransmitted();
176
long getInErrors();
177
long getOutRsts();
178
}
179
180
class UdpStats {
181
long getDatagramsSent();
182
long getDatagramsReceived();
183
long getDatagramsNoPort();
184
long getDatagramsReceivedErrors();
185
}
186
187
class IPConnection {
188
String getType();
189
String getLocalAddress();
190
int getLocalPort();
191
String getForeignAddress();
192
int getForeignPort();
193
TcpState getState();
194
int getOwningProcessId();
195
196
enum TcpState {
197
CLOSED, LISTEN, SYN_SENT, SYN_RECV, ESTABLISHED, FIN_WAIT_1,
198
FIN_WAIT_2, CLOSE_WAIT, CLOSING, LAST_ACK, TIME_WAIT, DELETE_TCB,
199
NONE, UNKNOWN
200
}
201
}
202
}
203
```
204
205
## Services
206
207
Operating system service information.
208
209
```java { .api }
210
interface OSService {
211
String getName();
212
int getProcessID();
213
State getState();
214
215
enum State {
216
RUNNING, STOPPED, START_PENDING, STOP_PENDING, CONTINUE_PENDING, PAUSE_PENDING, PAUSED
217
}
218
}
219
```
220
221
## User Sessions
222
223
Information about logged-in users.
224
225
```java { .api }
226
interface OSSession {
227
String getUserName();
228
String getTerminalDevice();
229
long getLoginTime();
230
String getHost();
231
}
232
```
233
234
## Desktop Windows
235
236
Desktop window information for GUI applications.
237
238
```java { .api }
239
interface OSDesktopWindow {
240
long getWindowId();
241
String getTitle();
242
String getCommand();
243
Rectangle getLocAndSize();
244
int getOwningProcessId();
245
long getOrder();
246
boolean isVisible();
247
}
248
```
249
250
## Installed Applications
251
252
Information about installed applications on the system.
253
254
```java { .api }
255
interface ApplicationInfo {
256
String getName();
257
String getVersion();
258
String getInstallDate();
259
}
260
```
261
262
## Usage Examples
263
264
### Basic OS Information
265
266
```java
267
import oshi.SystemInfo;
268
import oshi.software.os.OperatingSystem;
269
270
SystemInfo si = new SystemInfo();
271
OperatingSystem os = si.getOperatingSystem();
272
273
System.out.println("OS Family: " + os.getFamily());
274
System.out.println("Manufacturer: " + os.getManufacturer());
275
System.out.println("Version: " + os.getVersionInfo());
276
System.out.println("Bitness: " + os.getBitness() + "-bit");
277
System.out.println("Process Count: " + os.getProcessCount());
278
System.out.println("Thread Count: " + os.getThreadCount());
279
System.out.println("Uptime: " + os.getSystemUptime() + " seconds");
280
```
281
282
### Process Management
283
284
```java
285
import oshi.SystemInfo;
286
import oshi.software.os.OperatingSystem;
287
import oshi.software.os.OSProcess;
288
289
SystemInfo si = new SystemInfo();
290
OperatingSystem os = si.getOperatingSystem();
291
292
// Get all processes
293
List<OSProcess> processes = os.getProcesses();
294
295
// Get top 10 processes by CPU usage
296
List<OSProcess> topCpuProcesses = os.getProcesses(
297
OperatingSystem.ProcessFiltering.VALID_PROCESS,
298
OperatingSystem.ProcessSorting.CPU_DESC,
299
10
300
);
301
302
for (OSProcess process : topCpuProcesses) {
303
System.out.printf("PID: %d, Name: %s, CPU: %.1f%%, Memory: %d MB%n",
304
process.getProcessID(),
305
process.getName(),
306
process.getProcessCpuLoadCumulative() * 100,
307
process.getResidentSetSize() / 1024 / 1024
308
);
309
}
310
```
311
312
### Process Filtering and Sorting
313
314
```java
315
import oshi.SystemInfo;
316
import oshi.software.os.OperatingSystem;
317
import oshi.software.os.OSProcess;
318
319
SystemInfo si = new SystemInfo();
320
OperatingSystem os = si.getOperatingSystem();
321
322
// Get 64-bit processes only, sorted by memory usage
323
List<OSProcess> processes64bit = os.getProcesses(
324
OperatingSystem.ProcessFiltering.BITNESS_64,
325
OperatingSystem.ProcessSorting.RSS_DESC,
326
20
327
);
328
329
// Get child processes of a specific PID
330
List<OSProcess> children = os.getChildProcesses(
331
1234, // parent PID
332
OperatingSystem.ProcessFiltering.VALID_PROCESS,
333
OperatingSystem.ProcessSorting.NAME_ASC,
334
0 // no limit
335
);
336
```
337
338
### Network Configuration
339
340
```java
341
import oshi.SystemInfo;
342
import oshi.software.os.NetworkParams;
343
344
SystemInfo si = new SystemInfo();
345
NetworkParams netParams = si.getOperatingSystem().getNetworkParams();
346
347
System.out.println("Hostname: " + netParams.getHostName());
348
System.out.println("Domain: " + netParams.getDomainName());
349
System.out.println("IPv4 Gateway: " + netParams.getIpv4DefaultGateway());
350
System.out.println("IPv6 Gateway: " + netParams.getIpv6DefaultGateway());
351
352
String[] dnsServers = netParams.getDnsServers();
353
System.out.println("DNS Servers: " + Arrays.toString(dnsServers));
354
```
355
356
### User Sessions
357
358
```java
359
import oshi.SystemInfo;
360
import oshi.software.os.OSSession;
361
362
SystemInfo si = new SystemInfo();
363
List<OSSession> sessions = si.getOperatingSystem().getSessions();
364
365
for (OSSession session : sessions) {
366
System.out.printf("User: %s, Terminal: %s, Host: %s, Login Time: %s%n",
367
session.getUserName(),
368
session.getTerminalDevice(),
369
session.getHost(),
370
new Date(session.getLoginTime() * 1000)
371
);
372
}
373
```
374
375
### Current Process Information
376
377
```java
378
import oshi.SystemInfo;
379
import oshi.software.os.OperatingSystem;
380
import oshi.software.os.OSProcess;
381
382
SystemInfo si = new SystemInfo();
383
OperatingSystem os = si.getOperatingSystem();
384
385
// Get current process info
386
int currentPid = os.getProcessId();
387
OSProcess currentProcess = os.getCurrentProcess();
388
389
System.out.println("Current PID: " + currentPid);
390
System.out.println("Process Name: " + currentProcess.getName());
391
System.out.println("Command Line: " + currentProcess.getCommandLine());
392
System.out.println("Working Directory: " + currentProcess.getCurrentWorkingDirectory());
393
System.out.println("User: " + currentProcess.getUser());
394
```