在 Java 中查找物理机名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1100266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find physical machine name in Java
提问by ripper234
How can I obtain the physical machine name that my jvm is running in?
如何获取运行我的 jvm 的物理机名称?
(Physical = OS, up to vmware...)
(物理 = 操作系统,由 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.
从海报评论中添加:
我的意思是运行 JVM 的计算机的名称。很可能是一台物理计算机,但如果 JVM 在另一个虚拟机中运行,那么该名称很好。
采纳答案by jsight
String computername=InetAddress.getLocalHost().getHostName();
System.out.println(computername);
回答by jbu
I'm not exactly sure what you mean by Physical Machine Name. Your comment "(Physical = OS, up to vmware...)" needs explaining to me.
我不确定您所说的物理机名称是什么意思。您的评论“(物理 = 操作系统,由 vmware 决定...)”需要向我解释。
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()
但是您可以使用 System.getProperty(String key) 其中 key 是此处找到的键之一: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.
那应该告诉你操作系统名称。如果您需要主机名,请使用 jsight 的建议。
回答by Gandalf
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
或者在 Linux 上
Runtime r = Runtime.getRuntime();
Process p = r.exec("uname -a");
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream());
System.out.println(r.readLine());
回答by Andrew McKinlay
On Windows, if you want the workstation name, you can use:
在 Windows 上,如果您想要工作站名称,您可以使用:
System.getenv("COMPUTERNAME")
回答by Ondra ?i?ka
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.
根据虚拟化的级别,您可能会在裸机、虚拟机、VM 内部的 VM、Docker 甚至是某些云提供商的自定义 JVM 上运行,这些提供商可能具有额外的抽象级别和特定于云的网络处理应用程序接口。
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.
我最终使用了特定的云提供商的 API(AWS SDK 或 Google Cloud Engine API),如果它提供任何东西,或者网络 API(请参阅@jsight 的 anwser),并且,如果它提供了像 127.0.0.1 或 192.* 这样的 IP,那么我使用了hash()
保证在任何 JVM 实例中只出现一次的对象的 。
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.
所有这些变化都可能随着时间而改变。目标是在某个给定时刻唯一标识节点。如果这是您的最终目标,我希望这会有所帮助。