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:
- System memory, swap, cpu, load average, uptime, logins
- Per-process memory, cpu, credential info, state, arguments, environment, open files
- File system detection and metrics
- Network interface detection, configuration info and metrics
- 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
精彩评论