PeakGen
PeakGen

Reputation: 23045

Getting specific system information using environment variables

Please have a look at the following code

Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));}

Using this code snippet, I obtain all the information I need, as well as lot of NO-NEED information. To be honest, what I need is number of processors and processor identifier. These two are stored in environment variables PROCESSOR_IDENTIFIER , and NUMBER_OF_PROCESSORS, in Windows systems.

There is no way I can call this code

System.out.println("Processor Data: "+ System.getenv("PROCESSOR_IDENTIFIER")); 
    System.out.println("Number of Processors: "+System.getenv("NUMBER_OF_PROCESSORS"));

The reason is, oracle it self says the environment variables are differ from system to system. Which means, USERNAME windows environment variable is LOGIN or username in UNIX systems. Like that, it might have various other names in other systems.

But, I really don't need all the information that loop provides to me. I only need those two. I am thinking about using a code like below.

if("os.name" == "Windows")
{
System.out.println("Processor Data: "+ System.getenv("PROCESSOR_IDENTIFIER")); 
}

However, in that case, I don't know how many systems I need to check (I mean, do these environment variables changes from OS to OS or System to System? If I make it more clear, does these variables are same in Linux kernel or change across Ubuntu, Fedora, Red Hat etc?)

I have no idea about how get the only two data I need, by omitting the rest. Please help. I am very much glad to see code helps. Thank you

Upvotes: 0

Views: 2594

Answers (2)

Chris
Chris

Reputation: 5654

Can you try Runtime.getRuntime().availableProcessors(); ?

Upvotes: 2

Peter Svensson
Peter Svensson

Reputation: 6173

Number of CPU/cores: Finding Number of Cores in Java Dont think it's possible to get the CPU type in a convenient cross platform way.

Upvotes: 0

Related Questions