0

I have a GUI program that returns CPU values, this requires a external API. I have the JAR made, but the program will not return the CPU unless I run the program within netbeans IDE. My question is how can I pack APIs into a JAR file.

private class getCpu extends Thread {
        @Override
        public void run() {
            while(true) {
                try {
                Sigar sigar = new Sigar();
                Cpu cpu = new Cpu();
                CpuPerc perc = sigar.getCpuPerc();

                DecimalFormat df = new DecimalFormat("###.###");
                //df.format(100-(perc.getIdle()*100));
                cpuLabel.setText("CPU : "+df.format(100-(perc.getIdle()*100))+"%");

                try{Thread.sleep(1000);}catch(Exception e2){}
                }catch(SigarException e1){}
                catch(Exception e){}
            }
        }
    }
Arc
  • 441
  • 1
  • 9
  • 26

2 Answers2

0

Your program isn't working because you have no "external API" in your classpath when you run the program from command line (and you got ClassDefNotFoundException)

To solve this problem you can do one of:

  • add the external API jar to classpath: java -cp externalAPI.jar -jar jourProgram.jar

  • pack all necessary classes into your jar. There is a good maven plugin it can take all your dependencies and pack into a single jar file - https://maven.apache.org/plugins/maven-assembly-plugin/

m-szalik
  • 3,546
  • 1
  • 21
  • 28
  • The program opens when I execute from command line, but still it doesn't return the cpu unless I run it inside of NetBeans. Any ideas? – Arc Feb 23 '14 at 17:11
  • Any exceptions or error messages? Can you paste a command you are using to lunch the program from command-line? – m-szalik Feb 23 '14 at 17:15
  • No errors what-so-ever. I cd to the folder that has the jar then : java -jar GuiCpu.jar – Arc Feb 23 '14 at 17:21
  • But you do not add *the external API* to your classpath like: `-cp external.jar` There is no error because you probably "swallow" the exception. Paste the code responsible for receiving cpu load. – m-szalik Feb 23 '14 at 17:23
  • Ok I added the code, but on a side note the code works fine within netbeans, but when I build the jar in netbeans it will not work outside of netbeans. (it will open, but not get the cpu percentage) – Arc Feb 23 '14 at 17:30
  • So now I can see that you swallow all exceptions (there are empty `catch` statements - add `e.printStarcktrace()` in every catch block and you will see the cause of your problem). **Never swallow `Exception` because you will never know whats is wrong since no error will be reported.** You are using `Sigar API` but you forgot to add it to your classpath like this `-cp sigar.jar` – m-szalik Feb 23 '14 at 17:36
  • Ok sorry I figured out what I was doing wrong, thanks for your help. I'm posting an answer now to my question. – Arc Feb 23 '14 at 17:56
0

When using sigar's API you must add the log4j.jar and sigar.jar to your class-path. But also you need to add sigar-amd64-winnt.dll, sigar-x86-winnt.dll, and sigar-x86-winnt.lib to the final folder with the final build otherwise Sigar will not work properly.

Arc
  • 441
  • 1
  • 9
  • 26