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.

2
  • What platform? Does it matter if it's OS specific?
    – Gandalf
    Jul 8, 2009 at 20:07
  • Best is platform-independent of course.
    – ripper234
    Jul 9, 2009 at 5:40

5 Answers 5

58
String computername=InetAddress.getLocalHost().getHostName();
System.out.println(computername);
3
  • 7
    Note that this relies on DNS and can fail: stackoverflow.com/questions/7883542/…
    – Vadzim
    Dec 6, 2013 at 6: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 stackoverflow.com/a/40702767/1504556. Java has no (standardized) way to get "machine name" or "computer name" of the local machine.
    – peterh
    Nov 30, 2016 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, 2017 at 6:48
12

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

System.getenv("COMPUTERNAME")
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());
1
  • i was under the impression getRunTimeMXBean() is not 100% reliable
    – Hector
    Mar 13, 2013 at 5:01
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.

1
  • 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 9, 2009 at 5: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.