Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: SIGAR to find CPU info-problem

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question SIGAR to find CPU info-problem

    I tried SIGAR to find the CPU info. I used the following 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[i]);
            }
     
            println("Totals........");
            output(this.sigar.getCpuPerc());
        }
     
        public static void main(String[] args) throws Exception {
            new CpuInfo().processCommand(args);
        }
    }
    I have no error on compilation.But when i run the code, i got the following errors
    E:\java>java CpuInfo
    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(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            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)
            at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: CpuInfo.  Program will exit.
    I think i need to give command line arguements. but i dont know what data to be given as parameter. help me. thanks in advance.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: SIGAR to find CPU info-problem

    You need to make sure you have the sigar.jar file on your classpath so that when the JVM starts up to run your code, it will know where to find the class CpuInfo.

    For more information about classpath see Setting the class path

    // Json

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIGAR to find CPU info-problem

    Mr.Json, compiled my project using the following command, where "d:\java packages\hyperic-sigar-1.6.3\hyperic-sigar-1.6.3\sigar-bin\lib\sigar.jar" is the path of my sigar.jar.
    E:\java>javac CpuInfo.java -classpath "d:\java packages\hyperic-sigar-1.6.3\hype
    ric-sigar-1.6.3\sigar-bin\lib\sigar.jar"
    There is no error. But while running, i got the following error.

    E:\java>java CpuInfo -classpath "d:\java packages\hyperic-sigar-1.6.3\hyperic-si
    gar-1.6.3\sigar-bin\lib\sigar.jar"
    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(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    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)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: CpuInfo. Program will exit.
    pls help me

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: SIGAR to find CPU info-problem

    Do you have the .dll or .so files in the right place on your machine?

    // Json

  5. #5
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIGAR to find CPU info-problem

    Actually i am not aware whether the related dll files are in the right path. Can u help me to place them at the right path?

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: SIGAR to find CPU info-problem

    They need to be on the path somehow, like put the dll in windows/system32 or you could put it in your JDK/jre/lib folder or something like that.

    // Json

  7. #7
    Junior Member
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: SIGAR to find CPU info-problem

    Ya. i did it. again i am getting the same error .....

  8. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: SIGAR to find CPU info-problem

    are you using eclipse? If you are, use the import function of eclipse and import them directly into your project.

Similar Threads

  1. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM
  2. Replies: 5
    Last Post: September 19th, 2009, 06:48 AM
  3. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 10:33 AM
  4. [SOLVED] How to a set java class path?
    By captjade in forum Java Theory & Questions
    Replies: 1
    Last Post: March 10th, 2009, 06:40 AM

Tags for this Thread