more example stuff

This commit is contained in:
Doug MacEachern 2006-03-31 21:57:06 +00:00
parent a832ba371b
commit a20b0b853d
2 changed files with 62 additions and 1 deletions

View File

@ -1,6 +1,9 @@
import net.hyperic.sigar.*; import net.hyperic.sigar.*;
/* /*
Example to show the process state for a given pid.
Compile the example: Compile the example:
% javac -classpath sigar-bin/lib/sigar.jar ProcessState.java % javac -classpath sigar-bin/lib/sigar.jar ProcessState.java
@ -15,6 +18,9 @@ bash: Sleeping
State of emacs editor used to write the example: State of emacs editor used to write the example:
% java -classpath sigar-bin/lib/sigar.jar:. ProcessState 2673 % java -classpath sigar-bin/lib/sigar.jar:. ProcessState 2673
emacs: Suspended emacs: Suspended
See also: examples/Ps.java, examples/Top.java
*/ */
public class ProcessState { public class ProcessState {
@ -52,7 +58,6 @@ public class ProcessState {
ProcState procState = sigar.getProcState(pid); ProcState procState = sigar.getProcState(pid);
String state; String state;
System.out.println(procState.getName() + ": " + System.out.println(procState.getName() + ": " +
getStateString(procState.getState())); getStateString(procState.getState()));

View File

@ -0,0 +1,56 @@
import java.util.Arrays;
import java.util.List;
import net.hyperic.sigar.win32.Service;
import net.hyperic.sigar.win32.Win32Exception;
/*
Example to show the status of a Windows services.
Compile the example:
% javac -classpath sigar-bin/lib/sigar.jar ServiceStatus.java
Status of all services:
% java -classpath sigar-bin/lib/sigar.jar:. ServiceStatus
Alerter: Stopped
ALG: Running
Apache Tomcat 4.1: Stopped
Apache2: Running
...
Status of a specific service:
% java -classpath sigar-bin/lib/sigar.jar:. ServiceStatus Eventlog
Eventlog: Running
See also: examples/Win32Service.java
*/
public class ServiceStatus {
private static void printStatus(String name)
throws Win32Exception {
Service service = new Service(name);
System.out.println(name + ": " +
service.getStatusString());
service.close();
}
public static void main(String[] args)
throws Exception {
List services;
String name;
if (args.length == 0) {
services = Service.getServiceNames();
}
else {
services = Arrays.asList(args);
}
for (int i=0; i<services.size(); i++) {
printStatus((String)services.get(i));
}
}
}