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? Commented Jul 8, 2009 at 20:07
  • Best is platform-independent of course. Commented Jul 9, 2009 at 5:40

5 Answers 5

59
String computername=InetAddress.getLocalHost().getHostName(); System.out.println(computername); 
Sign up to request clarification or add additional context in comments.

3 Comments

Note that this relies on DNS and can fail: stackoverflow.com/questions/7883542/…
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.
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.
12

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

System.getenv("COMPUTERNAME") 

1 Comment

This is the NETBIOS name of the computer. It's the first 15 characters of the actual hostname. So they may not match.
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 Comment

i was under the impression getRunTimeMXBean() is not 100% reliable
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 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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.