3

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?

skiwi
  • 66,971
  • 31
  • 131
  • 216
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

5 Answers5

4

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.

Rohitdev
  • 866
  • 6
  • 15
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.

Mark W
  • 2,791
  • 1
  • 21
  • 44
  • Are there any text files similar to /proc in Linux? – Peter Penzov Apr 24 '14 at 15:14
  • I don't believe so. The registry stores most information for windows, and system information is available via task manager or MMC, but as far as im aware, task manager doesnt reference any virtual files like /proc. This might be helpful: http://stackoverflow.com/questions/5802143/is-there-anything-like-proc-for-windows – Mark W Apr 24 '14 at 15:17
  • I found this: http://stackoverflow.com/questions/62289/read-write-to-windows-registry-using-java Can I read these values from windows registry? – Peter Penzov Apr 24 '14 at 18:28
2

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

Community
  • 1
  • 1
xmojmr
  • 8,073
  • 5
  • 31
  • 54
  • Can you give me some more information about `newer Java bindings`? If you have opportunity would you show me some basic example? – Peter Penzov Apr 28 '14 at 17:43
  • @Peter I'm sorry for confusion. By "newer Java bindings" I meant just go through the Java→WMI bindings proposed in the "Recommended libraries.." linked article and check if there is a newer version available as the answer is little bit old. e.g the jWMI is from 2010 so perhaps contact Henry Ranch on his website to ask for newer version or known problems etc. I don't have personal practical experience with using WMI from Java so I will not help you much more (BTW: you can also create PowerShell script (it has full .NET access) and you call it as command line application) – xmojmr Apr 28 '14 at 18:22
  • @Peter quick Google redirected me to http://superuser.com/questions/176624/linux-top-command-for-windows-powershell where you can find more about Linux commands and their PowerShell alternatives.. – xmojmr Apr 28 '14 at 18:43
1

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:

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
ethanfar
  • 3,733
  • 24
  • 43
0

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.

Sambhav Sharma
  • 5,741
  • 9
  • 53
  • 95