Reputation: 150
I am trying to read some basic information about the device (in Solaris machine) like no. of CPUs, no. of HDDs, no. of Network adapters etc ...
From what I understand, we cannot get these information directly from Java. (Correct me if I am wrong).
So, the workaround is by calling JNI calls.
Now, how do I code this in C/C++ ? Is there any existing system call/ method in C to get these information ?
I am trying to avoid using system() method in C because I want to store the information as a string.
I am aware that there are shell commands like iostat, kstat, ifconfig etc ... But how do I simulate them in my C/C++ program ?
Thanks,
Upvotes: 0
Views: 2395
Reputation: 120376
For a Java solution, an alternative to JNI would be to use Java's Runtime.exec()
to execute system commands. Here's a simple example:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("/bin/cat /proc/cpuinfo");
process.waitFor();
InputStream in = process.getInputStream();
// read stdout contents from InputStream and parse accordingly
Note that this has disadvantages:
Upvotes: 1
Reputation: 28094
The /proc filesystem has lots of information for you to parse. iostat e.g. reads /proc/diskstats. There is much more there, and most of linuxs utilitiy programs just read the information from here.
You don't even need JNI to get all the info, at least, if you stay on linux or solaris.
Upvotes: 1
Reputation: 70949
To code this in JNI (bound to C)
javac
to compile the ".java"
file to a ".class" filejavah
tool to extract a C
/ C++ compatible header file from
the compiled ".class" file..so
for
unix / linux, .dll
for windows).As far as how you can get the desired data, refer to any number of systems programming references for Windows or UNIX. If targeting the Linux platform, most of the tools are open source, so you can look at the source code to determine how the tools obtained their data (assuming that you can live with the license restrictions that such a task might place on your code).
Upvotes: 0