需要在java中以某种方式识别本地机器 [英] Need to identify local machine somehow in java

查看:133
本文介绍了需要在java中以某种方式识别本地机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找正在运行应用程序的本地计算机的名称(或其他一种唯一标识特定计算机的方法)。

I'm trying to find out the name of the local machine (or some other way of uniquely identifying a particular machine) that an application is running on.

我一直在使用它:

String hostname = java.net.InetAddress.getLocalHost().getHostName();

工作正常,但我刚刚在剖析器中注意到它需要花费相当多的时间执行它所以我想知道是否有一些功能相似的东西运行得更快。

which works fine, but I just happened to notice in the profiler that it's taking quite a bit of time to execute that so I was wondering if there was something functionally similar that ran more quickly.

不必是主机名,可以是任何东西,只要它唯一识别机器。

Doesn't have to be host name, could be anything as long as it uniquely identifies the machine.

推荐答案

我想出了这个:

String dirname = Utils.safetrim(System.getProperty("user.dir"));

显然,世界上没有唯一识别机器但是出于我的目的它已经足够好了,因为每个人运行这个程序将自己登录,并可能从他们的主目录运行,所以它使我的响应足够独特。

It obviously doesn't world uniquely identify a machine but for my purposes it's good enough, because each person running this program will be logged in as themselves and probably be running from their home directory, so it makes the response unique enough for me.

当然它很快,这是重要的部分。

And of course it's wicked fast, which is the important part.

这篇关于需要在java中以某种方式识别本地机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆