This discussion is archived
1 2 Previous Next 19 Replies Latest reply: Nov 9, 2010 6:40 AM by PhHein RSS

Sigar api problems!

843810 Newbie
Currently Being Moderated
Hi!I'm kind a new to world of Java!I started to learn about java but im not a master(that's why i came here)!
I want to make an app that give me the current cpu load and available ram(oh i forgot to mention that i use Linux Mint 8 wich is based on Ubuntu 10.04)and i want my app to run on both linux and windows machine!So for that i tried to use the sigar api(system information gatherer and report) ,wich is a open source project that was written for java language but it have bindings for python,ruby perl....

So i started to look at some examples that they distribute and i choose the CpuInfo.java
Here's the code:
package org.hyperic.sigar.cmd;

import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarLoader;
import org.hyperic.sigar.SigarException;

/**
 * Display cpu information for each cpu found on the system.
 */
public class CpuInfo extends SigarCommandBase {

    public boolean displayTimes = true;
    
    public CpuInfo(Shell shell) {
        super(shell);
    }

    public CpuInfo() {
        super();
    }

    public String getUsageShort() {
        return "Display cpu information";
    }

    private void output(CpuPerc cpu) {
        println("User Time....." + CpuPerc.format(cpu.getUser()));
        println("Sys Time......" + CpuPerc.format(cpu.getSys()));
        println("Idle Time....." + CpuPerc.format(cpu.getIdle()));
        println("Wait Time....." + CpuPerc.format(cpu.getWait()));
        println("Nice Time....." + CpuPerc.format(cpu.getNice()));
        println("Combined......" + CpuPerc.format(cpu.getCombined()));
        println("Irq Time......" + CpuPerc.format(cpu.getIrq()));
        if (SigarLoader.IS_LINUX) {
            println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq()));
            println("Stolen Time...." + CpuPerc.format(cpu.getStolen()));
        }
        println("");
    }

    public void output(String[] args) throws SigarException {
        org.hyperic.sigar.CpuInfo[] infos =
            this.sigar.getCpuInfoList();

        CpuPerc[] cpus =
            this.sigar.getCpuPercList();

        org.hyperic.sigar.CpuInfo info = infos[0];
        long cacheSize = info.getCacheSize();
        println("Vendor........." + info.getVendor());
        println("Model.........." + info.getModel());
        println("Mhz............" + info.getMhz());
        println("Total CPUs....." + info.getTotalCores());
        if ((info.getTotalCores() != info.getTotalSockets()) ||
            (info.getCoresPerSocket() > info.getTotalCores()))
        {
            println("Physical CPUs.." + info.getTotalSockets());
            println("Cores per CPU.." + info.getCoresPerSocket());
        }

        if (cacheSize != Sigar.FIELD_NOTIMPL) {
            println("Cache size...." + cacheSize);
        }
        println("");

        if (!this.displayTimes) {
            return;
        }

        for (int i=0; i<cpus.length; i++) {
            println("CPU " + i + ".........");
            output(cpus);
}

println("Totals........");
output(this.sigar.getCpuPerc());
}

public static void main(String[] args) throws Exception {
new CpuInfo().processCommand(args);
}
}
Now i want to compile it:In terminal i type:
javac -cp "sigar.jar" CpuInfo.java
No error's so i go to run it and i type 
java CpuInfo
, but now start the problems:
Exception in thread "main" java.lang.NoClassDefFoundError: CpuInfo (wrong name: org/hyperic/sigar/cmd/CpuInfo)
     at java.lang.ClassLoader.defineClass1(Native Method)
     at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
     at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
     at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
     at java.security.AccessController.doPrivileged(Native Method)
     at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
     at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: CpuInfo. Program will exit.
Before i came here i done a lot of researche's and i found that the problem might be that my java machine cant find my sigar package(wich is sigar.jar).
So my question is how can i resolve these error's?
My sigar.jar is in the same directory with CpuInfo.java!

Please help me!
Thank's and please excuse my bad english skills!

Edited by: fioan89 on Mar 11, 2010 7:42 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 1. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    last time I tried to run my JAR on linux (Ubuntu), it took the following syntax: java -jar myJarFile.jar
  • 2. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    Yeap if i type
    java -jar sigar.jar CpuInfo
    my class run with no problem!

    As i said before i'm a newbie!
    Thank's a lot!

    Edited by: fioan89 on Mar 11, 2010 8:25 AM

    Edited by: fioan89 on Mar 11, 2010 8:26 AM
  • 3. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    hi fioan,

    i'm having the same problem with you.
    in ubuntu too.
    i can compile the program using java -cp sigar.jar
    but when i try to run using this: java -jar sigar.jar MemoryMonitor
    got this error: Unknown command: MemoryMonitor
    what the hell is that?
    I've been dealing with this problem for month.
    anybody can help?
  • 4. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    "


         
    hi fioan,

    i'm having the same problem with you.
    in ubuntu too.
    i can compile the program using java -cp sigar.jar
    but when i try to run using this: java -jar sigar.jar MemoryMonitor
    got this error: Unknown command: MemoryMonitor
    what the hell is that?
    I've been dealing with this problem for month.
    anybody can help?
    "

    Well first yo shall compile the file with this command
    javac -cp "sigar.jar" MemoryMonitor.java
    Be sure that you include that " " and you'r jar file is in the MemoryMonitor.java class

    Next you run the file with
    java -cp sigar.jar MemoryMonitor
    Be careful javac is for compiling the code!
    And java is for running the byte-code in jvm!
  • 5. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    hi fioan,

    i got this error:
    Exception in thread "main" java.lang.NoClassDefFoundError: MemoryMonitor/java
    Caused by: java.lang.ClassNotFoundException: MemoryMonitor.java
         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    Could not find the main class: MemoryMonitor.java. Program will exit.

    what else i missed out?
    I'm running this on ubuntu.
  • 6. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    Hmm.....my bad i wasn't so careful with the man pages
    Well forget about all that crap that i wrote above
    Here's the step:
    To compile a java file that use sigar api let's say MyJavaSigar.java you do this
    javac -classpath ./sigar.jar MyJavaSigar.java
    but now''s the hack:if you want to run you type this:
    java -classpath ./sigar.jar:. MyJavaSigar
    Now let me explain something:
    The command
    java -jar sigar.jar <param>
    doe'sn't affect you'r file,this code that i wrote above will run the sigar.jar with the command <param>

    So if you type the
    java -jar sigar.jar CpuInfo
    will run the CpuInfo class wich is implented in the sigar.jar package so in short i was fooled by the fact that my own class was named CpuInfo.java so when i try to run my class infact i run the class implemented in sigar.jar(that's why i get some strange result wich wasn't suppose to get)

    Now i hope this time everything will run ok but now came one problem again.I want my app that i wrote in Linux to run in windows.I dual boot into my Windows 7 i installed the jdk
    i cd to my files and run the command
    java -classpath ./sigar.jar:. MyJava
    but again this message :
    Exception in thread "main" java.lang.NoClassDefFoundError: CpuInfo
    Caused by: java.lang.ClassNotFoundException: CpuInfo
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: CpuInfo.  Program will exit.
    Hmm..well i said it doesn't cost me anything if i compile again the file so i type:
    javac -classpath ./sigar.jar MyJava.java
    and all seem ok but it doesn't work.

    In my working director i have the sigar.jar isn't this enough?
    Or what i should do more?

    Cheers!
  • 7. Re: Sigar api problems!
    DarrylBurke Guru Moderator
    Currently Being Moderated
    The classpath.path etc separator in *nix is the colon, for Windows it's the semicolon.  Try
    java -classpath ./sigar.jar;. MyJavaSigar
    db
  • 8. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    Thank's man.That almost work but now i get an error like this
    no sigar-x86-winnt.dll in java.library.path
    org.hyperic.sigar.SigarException: no sigar-x86-winnt.dll in java.library.path
    I never use a dll with java,anyway i have the dll and with some search on google i've got to a point but still nothing.Here's what i done:
    java -classpath ./sigar.jar;. -Djava.library.path=./sigar-x86-winnt.dll MyJavaSigar
    Now i'm going to make a deep search but if you could help than please don't hesitate!

    Thank's
  • 9. Re: Sigar api problems!
    DarrylBurke Guru Moderator
    Currently Being Moderated
    java.library.path is a path, not a file. It needs to point at the folder that contains the DLL.

    db
  • 10. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    hi fioan,

    I've done this on windows XP, not so sure if it works the same in windows 7.
    you can directly copy the sigar.jar and sigar-x86-winnt.dll and some other important library into the jdk library.
    in windows : go to program files->java->jdk1.6.0->lib
    copy all those important libraries into this lib.
    hope it works.
    all the best:D
  • 11. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    Yes you'r right that option want a location not a file i change that code into this
    java -Djava.library.path=.\\  -classpath .\\sigar.jar;. MyJavaSigar
    And all went ok.
    Thank's men!I finnaly get my app run on Linux and windows!
  • 12. Re: Sigar api problems!
    DarrylBurke Guru Moderator
    Currently Being Moderated
    you can directly copy the sigar.jar and sigar-x86-winnt.dll and some other important library into the jdk library.
    Bad advice. The OP is already on the right track, no need to muddy the waters here.

    db
  • 13. Re: Sigar api problems!
    DarrylBurke Guru Moderator
    Currently Being Moderated
    fioan89 wrote:
    Yes you'r right that option want a location not a file i change that code into this
    java -Djava.library.path=.\\  -classpath .\\sigar.jar;. MyJavaSigar
    And all went ok.
    Congratulations on learning to do it the right way. And please don't ever copy stuff into jdk and system folders like rose_joe, that's a quick fix that comes back to bite you later.

    db
  • 14. Re: Sigar api problems!
    843810 Newbie
    Currently Being Moderated
    Hi Darrylburke,

    thanks for the advice, what if i wanted to make all these program autorun from my thumb drive.
    Do I have to recompile using the same command? I mean in fioan situation.
1 2 Previous Next