28

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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
ripper234
  • 222,824
  • 274
  • 634
  • 905

5 Answers5

58
String computername=InetAddress.getLocalHost().getHostName();
System.out.println(computername);
jsight
  • 27,819
  • 25
  • 107
  • 140
  • 7
    Note that this relies on DNS and can fail: http://stackoverflow.com/questions/7883542/getting-the-computer-name-in-java – Vadzim Dec 06 '13 at 06:41
  • 2
    The way the question is phrased: "get physical machine name", this is *not* a correct answer. Strictly speaking what the above will give you is the **network name** of the host, typically the name of the localhost interface. See http://stackoverflow.com/a/40702767/1504556. Java has no (standardized) way to get "machine name" or "computer name" of the local machine. – peterh Nov 30 '16 at 17:04
  • totally agreed with @Vadzim . Without DNS or Dynamic DNS mapping the assigned IP address by DHCP, the computer name returned could be very different from the real computer name. – someone Mar 23 '17 at 06:48
12

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

System.getenv("COMPUTERNAME")
Andrew McKinlay
  • 2,431
  • 1
  • 24
  • 27
11

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());
Gandalf
  • 9,648
  • 8
  • 53
  • 88
1

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.

jbu
  • 15,831
  • 29
  • 82
  • 105
  • 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. – ripper234 Jul 09 '09 at 05:40
0

I think you can't find such name reliably.

Depending on the level of virtualization, you may be running on bare metal, virtual machine, VM inside of a VM, Docker, or even a custom JVM by some cloud provider which may have additional levels of abstraction and cloud-specific handling of the networking API.

I've been dealing with this in a recent task of identifying a machine to detect when some service instance gets stuck. This has to work both in a hosted test environment and in production in the cloud.

I ended up using the particular cloud provider's API (AWS SDK or Google Cloud Engine API) if it gives anything, or networking API (see @jsight's anwser), and, if that gives IP's like 127.0.0.1 or 192.*, then I used a hash() of an object that is guaranteed to be just once in any JVM instance.

All of these change may change over time. The goal was to uniquely identify the node at some given moment. If that's your ultimate goal, I hope this helps.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277