我想使用Java在Windows、MacOS和可能的Linux上获取与计算机唯一相关的id。可以是磁盘UUID、主板序列号等。
Runtime.getRuntime().exec
可以被使用(它不是一个applet)。
有什么想法吗?
我想使用Java在Windows、MacOS和可能的Linux上获取与计算机唯一相关的id。可以是磁盘UUID、主板序列号等。
Runtime.getRuntime().exec
可以被使用(它不是一个applet)。
有什么想法吗?
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
我用来列出它们的代码:
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
System.out.println(ni.getName() + " " + ni.getDisplayName());
}
在这个页面上提供的选项中,我认为最可接受的选择,也是我在解决方案中使用的选择是@Ozhan Duz提供的那个,另一个与@finnw答案类似的选择是使用JACOB,值得一提的是com4j - 示例可以使用WMI,可在此处找到:
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));
}
<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>
OSHI
项目提供了独立于平台的硬件工具。
Maven依赖:
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>LATEST</version>
</dependency>
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);
}
}
我的机器的输出结果:
Microsoft#57YRD12#Intel64 Family 6 Model 60 Stepping 3#8
您的输出将不同,因为至少处理器序列号将不同。
你想用这个唯一标识符做什么?也许你可以不用这个ID就实现你想要的功能。
MAC地址可能是一个选项,但这并不是一个可信的唯一标识符,因为用户可以更改计算机的MAC地址。
要获取主板或处理器ID,请查看此链接。
仅在Windows系统中,您可以使用WMI通过COM桥接(例如JACOB)获取主板ID。
示例:
import java.util.Enumeration;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
public class Test {
public static void main(String[] args) {
ComThread.InitMTA();
try {
ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");
Variant instances = wmi.invoke("InstancesOf", "Win32_BaseBoard");
Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
while (en.hasMoreElements())
{
ActiveXComponent bb = new ActiveXComponent(en.nextElement().getDispatch());
System.out.println(bb.getPropertyAsString("SerialNumber"));
break;
}
} finally {
ComThread.Release();
}
}
}
不清楚您所有的要求。例如,您是想从全世界所有计算机中唯一地识别一台计算机,还是只是想从应用程序用户集合中唯一地识别一台计算机。此外,您能在系统上创建文件吗?
如果您能够创建文件,则可以创建一个文件,并使用文件的创建时间作为唯一标识。如果在用户空间中创建,则它将唯一地识别特定机器上应用程序的用户。如果在全局某处创建,则可以唯一地识别该计算机。
同样,就像大多数事情一样,“快”到什么程度才算足够快...或者在这种情况下,“独特”到什么程度才算足够独特。
为了唯一地识别Windows机器。 使用wmic时,请确保有备选方法的策略。由于“wmic bios get serialnumber”可能无法在所有计算机上工作,您可能需要使用其他方法:
# Get serial number from bios
wmic bios get serialnumber
# If previous fails, get UUID
wmic csproduct get UUID
# If previous fails, get diskdrive serialnumber
wmic DISKDRIVE get SerialNumber
资源: 唯一标识Windows机器的最佳方法 http://www.nextofwindows.com/the-best-way-to-uniquely-identify-a-windows-machine/
如果任务是记录系统的唯一标识符,使用MAC id是最简单的方法。
虽然更改mac id是可能的,但只有在替换相应设备时才能更改系统的其他id。
因此,除非我们知道需要唯一标识符的目的,否则我们可能无法找到合适的解决方案。
然而,下面的链接有助于提取mac地址。 http://www.stratos.me/2008/07/find-mac-address-using-java/