开发者

Is it possible to gather information about computer hardware using Java?

开发者 https://www.devze.com 2023-01-14 08:21 出处:网络
If so, then how? I\'m doing a team project for school. I thought that Java couldn\'t actually access hardware directly, since that would make it hard to be cross-platform. I need to know this, because

If so, then how? I'm doing a team project for school. I thought that Java couldn't actually access hardware directly, since that would make it hard to be cross-platform. I need to know this, because after some quick Googling, I haven't found anything, and my team members(who want to do this and want to use Java) seem unsure o开发者_如何学运维f how to proceed- after apparently much more searching than I've done.


Your right in that you can't access hardware directly from Java (unless your calling on native code, but that's not what your after) since it runs in a sandboxed environment, namely the Java Virtual Machine (JVM).

However you can get some basic info from the JVM that it gathers from the underlying OS. Take a look at using Java to get OS-level system information


What you are looking for is SIGAR API

Overview

The Sigar API provides a portable interface for gathering system information such as:

  1. System memory, swap, cpu, load average, uptime, logins
  2. Per-process memory, cpu, credential info, state, arguments, environment, open files
  3. File system detection and metrics
  4. Network interface detection, configuration info and metrics
  5. TCP and UDP connection tables Network route table

This information is available in most operating systems, but each OS has their own way(s) providing it. SIGAR provides developers with one API to access this information regardless of the underlying platform. The core API is implemented in pure C with bindings currently implemented for Java, Perl, Ruby, Python, Erlang, PHP and C#.


The amount of info you'll be able to get using native Java API's is pretty small, as your program generally only knows about the VM it's sitting on.

You can, however, call out to the command line, run native apps, and parse the results. It's not particularly good form in a Java app, however, as you lose the cross-platform benefits usually associated with the language.


You can get some basic information regarding the Processor/s using System.getEnv().

You can also use Runtime.getRuntime() - See the response of R. Kettelerij for details.

Another option is to use JMX. The MemoryMXBean for example provides some information regarding the RAM usage (heap and non-heap).


To anyone looking for a library that is still being maintained as of 2023 check out OSHI

0

精彩评论

暂无评论...
验证码 换一张
取 消