views:

102

answers:

4

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 of how to proceed- after apparently much more searching than I've done.

+2  A: 

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

R. Kettelerij
A: 

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.

Sean McMains
A: 

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).

Eyal Schneider
JMX is used to get JVM related information?
卢声远 Shengyuan Lu
@Shengyuan Lu: you can use JMX to monitor many aspects of the runtime environment. For information regarding the currently running JVM, You can use the RuntimeMXBean. See http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/lang/management/RuntimeMXBean.html.
Eyal Schneider
+2  A: 

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#.

krmby
Thats an interesting way to approach the problem
TheLQ