Java: install, and use an API? (SIGAR)

I downloaded the Sigar API for Java( I want to use it in Java, but I don't know how. After I've downloaded it, where do I put it? Is there a certain folder in my java JDK I need to put it?

sort by: active | newest | oldest
1-10 of 24Next »
andy6 years ago

Libraries (an API) are normally provided as JAR files - a "Java Archive" file. This is a compressed file that you can think of in the same way as a zip except that you don't have to decompress it or anything like that.

JAR files are full of compiled java files - class files. A class file is what the computer runs when you run a java application. Each class file contains a java class. The files are organized into folders, which in java are called packages.

The way Java works is that it needs to know where the JAR file is. It does this with a "classpath" - literally the location of the external JAR or CLASS files that you are using. In a minute we will tell your programming software where to find the JAR.

In this particular case I would caution you that the API is a bit complex, but I'll go ahead and describe how to do as you ask. I'm assuming you are using windows? In this case you need to download the file from sourceforge. Then you need to unpack the Zip (it doesn't matter where) and find the following file hyperic-sigar-1.6.4\hyperic-sigar-1.6.4\sigar-bin\lib\sigar.jar - this file is our JAR.

Now In your programming software you need to right click on your project and select properties. There should be an entry in properties for something like "Java Build Path" (click it). Now you need to select an option that will read something like "Add External JARs". Now you just need to specify the JAR we identified earlier and you have imported the library.

Now the examples that you can find online for this library will work in that project.

Hope this helps,

p.s. If you have any further problems, could you provide some info on your operating system and which programming software you are using? Doing so would help me and others to help you.
metrogdor22 (author)  andy6 years ago
Thanks for the help. Yes, I'm using Windows XP, and NetBeans 6.9.1. I did what you said about the JAR(I've done that 3 times, as I've been told the same thing 3 times), but I still get the error "package org.hyperic.sigar does not exist; Unused Import" and then the other classes that it should contain all have their own error. And then at the bottom with my Main method, I get the error "class, interface, or enum expected".I've added a text file of the program here: Thanks again for the help!
Right, I think there are multiple problems here, none the least of which is that your program would not work, because;
  • the class has no constructor (which is not critical, but it is a good idea)
  • the class has no methods
  • the method calls to sigar like getVendor() should be on an instance of the sigar class, currently what you are actually doing is implicitly calling this.getVendor() in your CPUInfo class (it doesn't have this method)
  • there is no print statement, so you would not get the data out of your class
  • the static method needs to be inside the class declaration
Okay, here's how to fix it.
  • In the same folder as sigar.jar is log4j.jar. import both libraries at the same time, in the same way as described in my earlier post. (sigar.jar uses log4j, that's why we need it)
  • use the corrected version of your source code shown below (just copy paste)
package SigarTestPackage;

//import the classes that we will use
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

//declare the CPUInfo class
public class CPUInfo {

* declare a constructor for CPUINFO
* Remember that a constructor normally is meant to set the class
* this case we are bending the rules just to make the example clearer.
* Really the constructor should be empty and we should be doing this in a
* method.
public CPUInfo() {
// declare the Sigar class (we need this because it gathers the
// statistics)
Sigar sigar = new Sigar();

// the output string is just going to hold our output string
String output = "";

// an array of CPU Info classes. The CPUInfo class is a blank data
// holder class. Just like string it can hold any data.
CpuInfo[] cpuInfoList = null;

// the try catch block means that if we get an error we are notified
try {
// get the CPU information from the sigar library
cpuInfoList = sigar.getCpuInfoList();

// if something foes wrong
} catch (SigarException e) {
// write a description of the problem to the output

// exit the constructor

// for each item in the cpu info array
for (CpuInfo info : cpuInfoList) {
// add the data to the output ( output += "something" means add
// "something" to the end of output)
output += "\nCPU\n";
output += "Vendor: " + info.getVendor() + "\n";
output += "Clock: " + info.getMhz() + "Mhz\n";

// finally, print the data to the output

public static void main(String[] args) {
CPUInfo main = new CPUInfo();

The above should work, printing out Mhz and manufacturer info. Challenge; can you make it print out the "total cores", "cache size" and "model" as well?

metrogdor22 (author)  andy6 years ago
All I can say is thank you again, but I really am VERY thankful for people like you :)
No problem, any time (PM me if you start a new forum post for Java help but aren't getting any replies). I take it that you got it working?

metrogdor22 (author)  andy6 years ago
Working better than I had hoped in the first place, thank you very much(I made a change to make it output to a text area rather than the normal output area)! There's no problem with it, I'm just a bit curious as to why it outputs two of the same thing? When I run it, I get:

Vendor: AMD
Clock: 2004Mhz
Cores: 2

Vendor: AMD
Clock: 2004Mhz
Cores: 2

Again, this isn't a problem if it's not easily-fixable, but why is it doing that?
Good stuff... With regard to the double output, the documentation isn't clear but I'm almost certain your running a motherboard with dual cores. If you had a quad core it would have four entries (probably all the same). The cores can be set up differently, but normally there is no need to (that is why Sigar gets info on all the cores, rather than just telling you once).


p.s. If you want to get info on just the first core you need to replace the "for each" loop block with CpuInfo info = cpuInfoList[0]; (but keep the middle of the block)

am new to SIGAR I am trying to get data from hosts in my network(the applications that running on hosts) and write it to openTSDB, I've installed openTSDB on CentOS. So i have the monitoring system ready only need the data from hosts, if you can find any solution can help me it will be very appreciated.

This topic was dead for many years, so you really expect someone to reply now?

Dear Downunder35m,

I've searched and I've spent a lot of time to get any help but nothing helped me yet, until i found this discussion which is really useful to me, so yeah i am expecting someone to reply cuze not sure about anywhere else possible to get help

1-10 of 24Next »