4

I have searched all over the internet for about two days now. I have found a lot about how to find the CPU usage using VB and anything in the .NET framework. I am not using the .NET framework so these are irrelevant, I feel.

What I really need is results similar to the ones shown in the "CPU Usage" section in a Windows Task Manager under the "Performance" tab. If I could find a way to mimic that calculation, then that is exactly what I am looking for. I need the percentage of CPU Usage.

Obviously, Java is not the ideal language for finding the CPU usage of a computer, but that is the language I am using and I have been able to get everything else I need about the computer except this.

I am using Windows 7 with Java version 7 update 21. Eclipse is my IDE.

This is my first time posting a question on here, so if I have left anything out or something is not clear, please ask and I will do my best to explain more of what I mean.

Casey
  • 1,734
  • 2
  • 22
  • 32
  • http://stackoverflow.com/questions/47177/how-to-monitor-the-computers-cpu-memory-and-disk-usage-in-java possible duplicate – DGomez May 30 '13 at 15:54
  • @DGomez I tried using the SIGAR API that is mentioned in that answer that you posted, and I was able to get the memory of the computer and a few other things, but I am still not sure how to get the average CPU Usage. Maybe I just need some help in SIGAR API. What do you think? – Casey May 30 '13 at 16:02

1 Answers1

3

There are a couple of ways to do this; one is to run a command-line tool and parse the output. On Windows 7, you can run

c:\Windows\system32\typeperf "\processor(_total)\% processor time"

Try it -- it prints a few lines of sampled CPU load data.

Another way is to use OperatingSystemMXBean .

Ernest Friedman-Hill
  • 79,064
  • 10
  • 147
  • 183
  • That is a very nice command! Since it causes the output to come out continually, how could I change it to just out put one line only when I call it? – Casey May 30 '13 at 16:13
  • You can add "-sc 1" to print just one sample. – Ernest Friedman-Hill May 30 '13 at 16:28
  • This was exactly what I needed. Thank you for responding so quickly. I have marked this as the answer. I am going to parse through the output, as you said. – Casey May 30 '13 at 16:45
  • The `OperatingSystemMXBean` method does not work on my Windows 8 machine. I like your command-line solution. – Cardinal System Apr 26 '19 at 02:53