Find physical machine name in Java

40,067

Solution 1

String computername=InetAddress.getLocalHost().getHostName();
System.out.println(computername);

Solution 2

On Windows, if you want the workstation name, you can use:

System.getenv("COMPUTERNAME")

Solution 3

Couple options, since I'm not sure what you want:

RuntimeMXBean rmx = ManagementFactory.getRunTimeMXBean();
System.out.println(rmx.getName());

Or...

System.out.println(InetAddress.getLocalHost().getHostName());

Or on Linux

Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream());
System.out.println(r.readLine());

Solution 4

I'm not exactly sure what you mean by Physical Machine Name. Your comment "(Physical = OS, up to vmware...)" needs explaining to me.

But you can use System.getProperty(String key) where key is one of the keys found here: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()

That should tell you OS name. If you need hostname use jsight's advice.

Share:
40,067
ripper234
Author by

ripper234

See blog or LinkedIn Profile

Updated on April 19, 2020

Comments

  • ripper234
    ripper234 almost 4 years

    How can I obtain the physical machine name that my jvm is running in?

    (Physical = OS, up to vmware...)

    Added from poster's comment:
    I mean the name of the computer the JVM is running in. Most likely a physical computer, but if the JVM is running inside another virtual machine then that name is good.