0

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

PeakGen
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

2

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

Chris
  • 5,584
  • 9
  • 40
  • 58
0

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.

Community
  • 1
  • 1
Peter Svensson
  • 6,105
  • 1
  • 31
  • 31
  • That is JVM. The case I can't go with this JVM one is, one single false info = Millions of users fixing computers in a wrong way..It is a very bad thing!!! – PeakGen Sep 10 '12 at 06:18