首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Java中获得唯一的计算机标识符(如磁盘ID或主板ID)?

问如何在Java中获得唯一的计算机标识符(如磁盘ID或主板ID)?
EN

Stack Overflow用户
提问于 2010-01-01 03:15:39
回答 11查看 110.2K关注 0票数 67

我想要一台装有Java的电脑的唯一id,在Windows,MacOS上,如果可能的话,还有Linux。可能是磁盘UUID,主板序列号...

可以使用Runtime.getRuntime().exec (它不是小程序)。

想法?

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2010-01-01 03:31:51

通常使用与网卡相关联的MAC地址。

在Java 6中,可以通过以下API获得该地址:

Java 6 Docs for Hardware Address

我还没有在Java中使用过它,但对于其他网络识别应用程序,它很有帮助。

票数 22
EN

Stack Overflow用户

发布于 2012-10-18 00:42:08

MAC地址的问题是可能有许多网络适配器连接到计算机。大多数最新的手机默认都有两个(wi-fi +有线)。在这种情况下,必须知道应该使用哪个适配器的MAC地址。我在我的系统上测试了MAC解决方案,但我有4个适配器(电缆,WiFi,虚拟盒子的TAP适配器和一个蓝牙),我不能决定我应该采取哪一个MAC…如果决定使用当前正在使用的适配器(已分配地址),则会出现新的问题,因为有人可以将他/她的膝上型计算机从电缆适配器切换到wi-fi。在这种情况下,通过电缆连接笔记本电脑时存储的MAC现在将无效。

例如,这些是我在系统中找到的适配器:

代码语言:javascript
复制
lo MS TCP Loopback interface
eth0 Intel(R) Centrino(R) Advanced-N 6205
eth1 Intel(R) 82579LM Gigabit Network Connection
eth2 VirtualBox Host-Only Ethernet Adapter
eth3 Sterownik serwera dostepu do sieci LAN Bluetooth

我用来列出它们的代码:

代码语言:javascript
复制
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
    NetworkInterface ni = nis.nextElement();
    System.out.println(ni.getName() + " " + ni.getDisplayName());
}

从这个页面上的选项中,我最能接受的,我在我的解决方案中使用的是@Ozhan Duz,另一个类似于他使用雅各布的@finnw answer,值得一提的是com4j - sample,它使用了WMI is available here

代码语言:javascript
复制
ISWbemLocator wbemLocator = ClassFactory.createSWbemLocator();
ISWbemServices wbemServices = wbemLocator.connectServer("localhost","Root\\CIMv2","","","","",0,null);
ISWbemObjectSet result = wbemServices.execQuery("Select * from Win32_SystemEnclosure","WQL",16,null);
for(Com4jObject obj : result) {
    ISWbemObject wo = obj.queryInterface(ISWbemObject.class);
    System.out.println(wo.getObjectText_(0));
}

这将打印一些计算机信息和计算机序列号。请注意,此示例所需的所有类都必须由maven-com4j-plugin生成。maven-com4j-plugin配置示例:

代码语言:javascript
复制
<plugin>
    <groupId>org.jvnet.com4j</groupId>
    <artifactId>maven-com4j-plugin</artifactId>
    <version>1.0</version>
    <configuration>
        <libId>565783C6-CB41-11D1-8B02-00600806D9B6</libId>
        <package>win.wmi</package>
        <outputDirectory>${project.build.directory}/generated-sources/com4j</outputDirectory>
    </configuration>
    <executions>
        <execution>
            <id>generate-wmi-bridge</id>
            <goals>
                <goal>gen</goal>
            </goals>
        </execution>
    </executions>
</plugin>

上面的配置将告诉插件在项目文件夹的target/generated-sources/com4j目录中生成类。

对于那些想要看到现成的解决方案的人,我提供了三个类的链接,这些类是我在Windows,Linux和Mac上为获得machine SN而编写的:

票数 50
EN

Stack Overflow用户

发布于 2016-06-08 22:17:25

OSHI项目提供了platform-independent硬件实用程序。

Maven依赖:

代码语言:javascript
复制
<dependency>
    <groupId>com.github.oshi</groupId>
    <artifactId>oshi-core</artifactId>
    <version>LATEST</version>
</dependency>

例如,您可以使用类似以下代码的代码来唯一地标识一台计算机:

代码语言:javascript
复制
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.ComputerSystem;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OperatingSystem;

class ComputerIdentifier
{
    static String generateLicenseKey()
    {
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
        CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
        ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem();

        String vendor = operatingSystem.getManufacturer();
        String processorSerialNumber = computerSystem.getSerialNumber();
        String processorIdentifier = centralProcessor.getIdentifier();
        int processors = centralProcessor.getLogicalProcessorCount();

        String delimiter = "#";

        return vendor +
                delimiter +
                processorSerialNumber +
                delimiter +
                processorIdentifier +
                delimiter +
                processors;
    }

    public static void main(String[] arguments)
    {
        String identifier = generateLicenseKey();
        System.out.println(identifier);
    }
}

我的机器的输出:

代码语言:javascript
复制
Microsoft#57YRD12#Intel64 Family 6 Model 60 Stepping 3#8

您的输出会有所不同,因为至少处理器序列号会有所不同。

票数 33
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1986732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档