`
dreamoftch
  • 浏览: 498089 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java 获取 电脑 主板和硬盘序列号

阅读更多
When you need to know hardware details, Java is not the best tool unless you call a JNI routine or an external utility. The JNI solution is always the best because it is designed to interact closely with Java but it may be more complex to develop. If your need is simple (no interaction) and the need to be cross plateform is not present then calling an external utility is maybe "a good enough" choice.
如上所述,要想获取硬件信息,java不是最好的方式。。。
In these 2 examples, we create the appropriate VBS script file on-the-fly and capture its output. They are very Windows oriented since they rely on the "Windows Script Host" to execute the generated scripts.

The vbscript queries a WMI class to get a specific hardware information. Here we are using the Win32_BaseBoard but they are many others, see http://msdn2.microsoft.com/en-us/library/aa389273.aspx for complete list.
Motherboard serial number
//通过vbscript的方式获取主板序列号
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class MiscUtils {
  private MiscUtils() {  }

  public static String getMotherboardSN() {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs =
         "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
        + "Set colItems = objWMIService.ExecQuery _ \n"
        + "   (\"Select * from Win32_BaseBoard\") \n"
        + "For Each objItem in colItems \n"
        + "    Wscript.Echo objItem.SerialNumber \n"
        + "    exit for  ' do the first cpu only! \n"
        + "Next \n";

      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }

  public static void main(String[] args){
    String cpuId = MiscUtils.getMotherboardSN();
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, cpuId, "Motherboard serial number",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}

Hard disk serial number
//获取硬盘序列号
This serial number is created by the OS where formatting the drive and it's not the manufacturer serial number. It's unique, because it is created on the fly based on the current time information. AFAIK, there is no API that return that the manufacturer SN. At best, the SN of the HD firmware can be read but this will involve some very low-level API calls. Keep in mind that even if you get that number, there is no warranty that it will be unique since each manufacturer can assign the SN as they wish.

import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class DiskUtils {
  private DiskUtils() {  }

  public static String getSerialNumber(String drive) {
  String result = "";
    try {
      File file = File.createTempFile("realhowto",".vbs");
      file.deleteOnExit();
      FileWriter fw = new java.io.FileWriter(file);

      String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
                  +"Set colDrives = objFSO.Drives\n"
                  +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
                  +"Wscript.Echo objDrive.SerialNumber";  // see note
      fw.write(vbs);
      fw.close();
      Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
      BufferedReader input =
        new BufferedReader
          (new InputStreamReader(p.getInputStream()));
      String line;
      while ((line = input.readLine()) != null) {
         result += line;
      }
      input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result.trim();
  }

  public static void main(String[] args){
    String sn = DiskUtils.getSerialNumber("C");
    javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
         null, sn, "Serial Number of C:",
         javax.swing.JOptionPane.DEFAULT_OPTION);
  }
}

 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UserInfoUtil {

	/**
	 * 获取IP
	 */
	public static String getIp() {
		String ip = "";
		try {
			InetAddress in = InetAddress.getLocalHost();
			ip = in.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}
	/**
	 * 获取mac地址
	 */
	public static String getMacAddr() {
		String macAddr = "";
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			NetworkInterface dir = NetworkInterface.getByInetAddress(addr);
			byte[] dirMac = dir.getHardwareAddress();

			int count = 0;
			for (int b : dirMac) {
				if (b < 0)
					b = 256 + b;
				if (b == 0) {
					macAddr = macAddr.concat("00");
				}
				if (b > 0) {

					int a = b / 16;
					if (a == 10)
						macAddr = macAddr.concat("A");
					else if (a == 11)
						macAddr = macAddr.concat("B");
					else if (a == 12)
						macAddr = macAddr.concat("C");
					else if (a == 13)
						macAddr = macAddr.concat("D");
					else if (a == 14)
						macAddr = macAddr.concat("E");
					else if (a == 15)
						macAddr = macAddr.concat("F");
					else
						macAddr = macAddr.concat(String.valueOf(a));
					a = (b % 16);
					if (a == 10)
						macAddr = macAddr.concat("A");
					else if (a == 11)
						macAddr = macAddr.concat("B");
					else if (a == 12)
						macAddr = macAddr.concat("C");
					else if (a == 13)
						macAddr = macAddr.concat("D");
					else if (a == 14)
						macAddr = macAddr.concat("E");
					else if (a == 15)
						macAddr = macAddr.concat("F");
					else
						macAddr = macAddr.concat(String.valueOf(a));
				}
				if (count < dirMac.length - 1)
					macAddr = macAddr.concat("-");
				count++;
			}

		} catch (UnknownHostException e) {
			macAddr = e.getMessage();
		} catch (SocketException e) {
			macAddr = e.getMessage();
		}
		return macAddr;
	}
	/**
	 * 获取主板序列号
	 */
	public static String getMotherboardSN() {
		String result = "";
		try {
			File file = File.createTempFile("realhowto", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n" + "Set colItems = objWMIService.ExecQuery _ \n"
					+ "   (\"Select * from Win32_BaseBoard\") \n" + "For Each objItem in colItems \n" + "    Wscript.Echo objItem.SerialNumber \n"
					+ "    exit for  ' do the first cpu only! \n" + "Next \n";

			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.trim();
	}
	
}

 

 

 www.rgagnon.com/javadetails/java-0580.html

分享到:
评论

相关推荐

    java获取硬盘序列号的两种方法

    在Java编程语言中,获取硬盘序列号是一项常见的需求,...综上所述,Java获取硬盘序列号可以通过WMI查询或访问注册表来实现,但要注意平台兼容性和API稳定性问题。在实际开发中,根据项目需求和目标平台选择合适的方法。

    用JAVA读取硬盘序列号

    因此,如果你需要在较新版本的Java中获取硬盘序列号,可能需要寻找其他的解决方案,如JNI或使用第三方库。 JNI允许Java代码直接调用操作系统级别的函数,例如C语言编写的系统库,这些库通常提供了获取硬件信息的...

    linux系统java通过jni方式获取硬盘序列号

    linux系统java通过jni方式获取硬盘序列号。包括makefile代码可以直接编译运行,代码解释请参考我的博客文章 http://blog.csdn.net/starter110/article/details/8186788

    java读取硬盘序列号例子

    在Java编程环境中,读取硬盘序列号是一项常见的需求,特别是在系统管理、设备识别或软件授权等领域。硬盘序列号是每个硬盘制造商赋予的唯一标识符,它可以帮助我们区分不同的硬盘。以下是一个详细的Java实现方法,...

    java-getDisk.rar_Java 获取主板ID_java获取电脑ID

    在Java编程语言中,获取计算机硬件信息,如主板ID(也称为系统UUID)和硬盘分区编号,是一项常见的任务,尤其在系统管理和软件授权等领域。以下将详细解释如何使用Java实现这些功能。 首先,主板ID是计算机主板上的...

    java获取硬盘序列号(带源码)

    本程序的DLL文件是从网上获取,本人只做了一点小修改, Dll文件是用c++编写的,有需要源码的,可以联系我。 java的源码都给了。。。。 本程序是用JNI技术实现的读取硬盘序列号 public static String DiskID.Factory...

    JAVA 本地调用源码(自己写的,java获取电脑硬盘序列号)

    做了软件,有的时候又怕别人copy ,写一个获取电脑硬盘序列号的一个小程序,只获取电脑的硬盘序列号,MAC地址可以换,但是电脑的硬盘应该不会老换吧 呵呵 拿上来分享,大家看看 有没有什么更好的办法,一起研究研究...

    硬盘序列号修改工具 任意修改

    在IT领域,硬盘序列号(硬盘ID)是由硬盘制造商分配给每个硬盘的独特标识符,通常用于追踪和验证硬件的合法性。这种修改行为通常与数据隐私、安全性和合法性问题相关。 描述中提到“用于修改逻辑序列号”,这可能是...

    查看硬盘序列号的java程序

    在Java中获取硬盘序列号涉及到操作系统接口的调用,这通常通过Java的`java.io.File`类和`com.sun.management.OperatingSystemMXBean`接口来实现。 首先,我们需要引入`com.sun.management.OperatingSystemMXBean`...

    JAVA获取计算机硬盘序列号、分区卷标号、MAC地址、IP地址、计算机名称

    该jar工具包是通过DiskID.dll获取计算机硬盘序列号、分区卷标号、MAC地址、IP地址、计算机名称等的信息,获取内容如下: 计算机名称:201709071714 硬盘序列号:183534442995 C分区卷标号:29F513CB MAC地址:F0-A9-59-...

    java读取硬盘序列号(jni方式)

    总结来说,通过JNI和C/C++,Java程序员可以读取硬盘序列号,从而实现对硬件信息的访问。这个过程涉及到Java和本地代码的交互,以及对Windows API的深入理解。虽然这种方法相对复杂,但对于需要获取操作系统底层信息...

    用JAVA读取硬盘序列号 -源码.zip

    在Java中,没有内置的方法可以直接获取硬盘序列号,所以我们需要借助于Java Native Interface (JNI) 或者第三方库来实现。 JNI允许Java代码调用本地(操作系统级别的)函数,比如Windows API。然而,这种方式需要...

    获取硬盘物理序列号

    这将会列出所有连接到电脑上的硬盘的物理序列号。对于Linux用户,可以使用`hdparm`命令,例如: ```bash sudo hdparm -I /dev/sda | grep Serial ``` 这里的`/dev/sda`需要替换为实际的硬盘设备名。在MacOS系统中...

    java获取电脑的硬件信息代码

    本程序通过java准确获取电脑的硬件信息,中间用到第三方包,也一并放在本压缩文件中,里面有两个DLL动态链接库问价,要把这两个文件放在系统盘的SYSTEM32文件夹下面,合理配置第三方包就可以运行这个程序来获得电脑...

    获取硬盘物理序列号及型号

    在IT领域,硬件信息的获取是系统管理和故障排查的重要环节,特别是硬盘的物理序列号和型号,它们是硬盘独一无二的标识符,对于诊断硬盘问题、防止数据盗取以及跟踪设备等有着重要作用。本文将深入探讨如何获取硬盘的...

    Java获取系统CPU、内存、硬盘等系统信息

    本文将详细探讨如何利用Java获取Windows和Linux系统的CPU、内存以及硬盘信息。 首先,让我们聚焦于CPU信息的获取。在Java中,`java.lang.management`包提供了ManagementFactory类,它包含了获取系统管理信息的方法...

Global site tag (gtag.js) - Google Analytics