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 24 of 24

Thread: getProcessCPUTime()

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default getProcessCPUTime()

    Hi.
    What is the error at line 42?

    import java.io.*;
    import java.util.*;
    import com.sun.management.OperatingSystemMXBean;
    import java.lang.management.ManagementFactory;
    import java.lang.System;
     
        public class sysinfoHooshi{
     
    	public static void prt(String s){System.out.print(s);}
    	public static void main(String args[]) throws Exception{
    		int k = 10;//k recent pieces of info will be maintained
     
    		OperatingSystemMXBean osBean =
    		 (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
    		class Info{			
    			public String name;
    			public String version;
    			public String arch;
    			long CPUSpeed;
    		};
    		Info info =  new Info();
    		Info[] queue = new Info[100];
     
    		int pos = 1;
     
    		Date d = new Date();
    		long start = d.getTime();
     
    		while(true){
    			long time = d.getTime();
    			if( (start-time)%3 == 0 )
    			{
    				queue[pos].name = System.getProperty("os.name");
    				System.out.println( queue[pos].name);
    				queue[pos].version = System.getProperty("os.version");
    				queue[pos].arch = System.getProperty("os.arch");
    				pos++;
    			}
     
    		}
     
    		info.CPUSpeed = OperatingSystemMXBean.getProcessCPUTime();//javac ERROR!!
     
     
     
    	} // end main
       }// end class date

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: getProcessCPUTime()

    You tell us. What is the error at line 42?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    My terminal screen reads: (I am on ubuntu 12.04 LTS):
    ~$ javac sysinfoHooshi.java
    sysinfoHooshi.java:42: cannot find symbol
    symbol : method getProcessCPUTime()
    location: interface com.sun.management.OperatingSystemMXBean
    info.CPUSpeed = OperatingSystemMXBean.getProcessCPUTime();
    ^
    1 error
    But I apparently have imported that. Isn't Java 'import' = C++ using?

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: getProcessCPUTime()

    One problem I see is the following:
    Info[] queue = new Info[100];
    ...
    info.CPUSpeed = ... ;
    What do you think is going to happen here?

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by hooshdar3 View Post
    But I apparently have imported that. Isn't Java 'import' = C++ using?
    Take a look at the API: OperatingSystemMXBean (Java Platform SE 8 )

    That class does not contain that method. In fact, it doesn't contain any static methods, which is how you're trying to call it.

    --- Update ---

    Quote Originally Posted by Cornix View Post
    One problem I see is the following:
    Info[] queue = new Info[100];
    ...
    info.CPUSpeed = ... ;
    What do you think is going to happen here?
    This is what he thinks is going to happen:

    class Info{			
    			public String name;
    			public String version;
    			public String arch;
    			long CPUSpeed;
    		};
    		Info info =  new Info();

    The info.CPUSpeed isn't really wrong, although the setup is a little strange. The problem is in the right side of that line.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by KevinWorkman View Post
    Take a look at the API: OperatingSystemMXBean (Java Platform SE 8 )

    That class does not contain that method. In fact, it doesn't contain any static methods, which is how you're trying to call it.

    --- Update ---



    This is what he thinks is going to happen:

    class Info{			
    			public String name;
    			public String version;
    			public String arch;
    			long CPUSpeed;
    		};
    		Info info =  new Info();

    The info.CPUSpeed isn't really wrong, although the setup is a little strange. The problem is in the right side of that line.
    OK.I changed line42 to:

    info.CPUSpeed = ((OperatingSystemMXBean)getOperatingSystemMXBean() ).getProcessCpuTime();
    after the 2nd code snippet on:
    java - get OS-level system information - Stack Overflow

    But I get more errors
    (I have also imported java.lang.management.OperatingSystemMXBean with
    import java.lang.management.OperatingSystemMXBean;
    )
    Last edited by hooshdar3; July 9th, 2014 at 09:00 AM.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: getProcessCPUTime()

    I'm really not sure what you think that's supposed to do. Answer these questions:

    What functions are in the OperatingSystemMXBean class?

    What *static* functions are in the OperatingSystemMXBean class?

    What function are you trying to access? Are you trying to access it statically?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by KevinWorkman View Post
    I'm really not sure what you think that's supposed to do. Answer these questions:

    What functions are in the OperatingSystemMXBean class?

    What *static* functions are in the OperatingSystemMXBean class?

    What function are you trying to access? Are you trying to access it statically?
    I want to get CPU speed + Memory usage and maybe other administrating information

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by hooshdar3 View Post
    I want to get CPU speed + Memory usage and maybe other administrating information
    That doesn't answer any of the questions I asked.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    I just wanna use 'getProcessCpuTime()

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by hooshdar3 View Post
    I just wanna use 'getProcessCpuTime()
    Okay, and what class does that belong to?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by KevinWorkman View Post
    Okay, and what class does that belong to?
    It says it is part of OperatingSystemMXBean interface, but I don't know how to use interfaces

    --- Update ---

    I managed to get itt compiled and run, by adding:
    import com.sun.management.OperatingSystemMXBean;
    .
    .
    .
    OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean)
    ManagementFactory.getOperatingSystemMXBean();

    info.CPUSpeed = osBean.getProcessCpuTime();

    System.out.println(info.CPUSpeed);
    But I don't know How exactoly to get the CPU frequency.

  13. #13
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Please....What does the function do exactly?I cannot understand the document completely

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getProcessCPUTime()

    Please copy the text of the doc that you don't understand and post it here with your questions about it.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by Norm View Post
    Please copy the text of the doc that you don't understand and post it here with your questions about it.
    I don't understand what does it mean that the function
    Returns the CPU time used by the process on which the Java virtual machine is running in nanoseconds.
    and what's the use of it

  16. #16
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default getSystemCpuload()

    Hi.
    What's wrong with my invokation of getSystemCpuLoad?
    Here is How my code looks like:
    OperatingSystemMXBean osBean = (com.sun.management.OperatingSystemMXBean)
    			ManagementFactory.getOperatingSystemMXBean();
     
    double CPUUtil = ( osBean.getSystemCpuLoad() ) * 100;

    and here is the output from javac:
    $ javac sysinfoHooshi.java
    sysinfoHooshi.java:43: cannot find symbol
    symbol : method getSystemCpuLoad()
    location: interface com.sun.management.OperatingSystemMXBean
    queue[pos].CPUUtil = ( osBean.getSystemCpuLoad() ) * 100;
    ^
    1 error

  17. #17
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: getSystemCpuload()

    The code that you show and the error you posted do not belong together.
    Read the error message, it tells you what the problem is:
    queue[pos].CPUUtil = ( osBean.getSystemCpuLoad() ) * 100;
    Is wrong because the variable "queue" is not a known symbol.

  18. #18
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getSystemCpuload()

    Quote Originally Posted by Cornix View Post
    The code that you show and the error you posted do not belong together.
    Read the error message, it tells you what the problem is:

    Is wrong because the variable "queue" is not a known symbol.
    no.'queue' is defined:
    class Info{
    public String name;
    public String version;
    public String arch;
    public double ramUtil;
    public double CPUUtil;
    };

    Info[] queue = new Info[queueSize];

    int pos = 0;

    for(int i=0;i<queueSize;++i)
    queue[i] = new Info();
    the problem is in the RHS of the assignment

  19. #19
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: getSystemCpuload()

    Please post your code in code tags.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getProcessCPUTime()

    Don'r use it if you don't know what its used for.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Quote Originally Posted by Norm View Post
    Don'r use it if you don't know what its used for.
    But I need to get CPU speed.
    Last edited by hooshdar3; July 14th, 2014 at 08:50 AM.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: getProcessCPUTime()

    I need to get CPU speed.
    Have you asked an internet Search engine how to do that?

    You may have to use JNI to get hardware stats.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Mar 2014
    Posts
    70
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Default Re: getProcessCPUTime()

    Yes Norm I have. What is JNI and how to use it?

  24. #24
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: getProcessCPUTime()

    JNI stands for Java Native Interface. Its an API that allows you to use native libraries.

  25. The Following User Says Thank You to Cornix For This Useful Post:

    hooshdar3 (July 15th, 2014)