How to get Windows load values [duplicate]

核能气质少年 提交于 2019-12-30 10:38:13

问题


I would like using Java to get data from Windows about CPU, RAM, HDD, Network and list of processes(services). How I can get these system values values without using third-party libraries or JNI?

Is there something similar to /proc in Windows where I can get all necessary values and use them?


回答1:


You can use native API's using JNI.

You can also use SIGAR which is Apache licensed library which does much more then what you are asking for.It has wrapped the native calls in Java API's so that you dont have to know the inner workings of JNI.

You can also spawn processes from inside the code to run OS specific commands so that you can collect the system stats e.g. systeminfo |find "Available Physical Memory" or wmic command on windows or Linux cat /proc/meminfo . This can be achieved via java.lang.ProcessBuilder.




回答2:


I dont think you can get everything you want from a single location in the standard java API, but you could use the following classes for some of your requirements:

For the number of processors / processor stats / memory, etc:

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html\

For information about physical disks and whatnot:

http://docs.oracle.com/javase/7/docs/api/java/io/File.html

Another, really, really ugly option would be to make platform specific invokations of utilities via a Process object, and parse their output. EG invoke the top command via a process, read and parse its output.




回答3:


There is Windows Management Instrumentation API (WMI) for this kind of stuff. See e.g. .NET bindings here: MSDN - System.Management Namespace

Older Stack Overflow answer to similar question is available here: Recommended libraries/howtos for using WMI with java?

Look for newer Java bindings. There used to be J# as one of the .NET languages that was source-level compatible with Java. So in theory there should not be a big problem to find suitable Java→.NET→WMI bridge

This API is very powerful and besides some basic stuff like processes, sound devices, disk drives etc. you can access approximately same set of information that you can get by running msinfo32.exe.

So this is not Java-specific answer this is Windows-specific answer. All Windows versions support .NET and .NET can access that information through WMI easily.

For instance list of processes can be accessed easily through MSDN - System.Diagnostics.Process.GetProcesses

There used to be special Win32 APIs for native binaries and though still supported they are the history now. Piping output from one command line app to input of another is not the usual way to connect processes. In case this is really needed there is Microsoft Technet - Scripting with Windows PowerShell

I hope this will help you find some solution of your problem




回答4:


There are several options for getting system information in Java (the Runtime class, the java.lang.management package, etc.).

However, this is usually very limited information, and tends not to cover the things you seem to require.

You should consider using a 3rd party solution, such as the following:

  • SIGAR - http://www.hyperic.com/products/sigar (requires native DLL to be installed)
  • OSHI - https://github.com/oshi/oshi



回答5:


Well, I would suggest you to try JavaSysMon:

https://github.com/jezhumble/javasysmon

Manage OS processes and get cpu and memory stats cross-platform in Java. So the good thing about it is , its cross-platform. Currently it supports Mac OS X, Linux, Windows, and Solaris. So that is a positive. If this can do your work, you can actually use the cross-platform feature for your application.

Maybe its not as good as Sigar, but give it a try.



来源:https://stackoverflow.com/questions/23222251/how-to-get-windows-load-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!