`
dreamoftch
  • 浏览: 496643 次
  • 性别: 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中获取硬盘序列号,可能需要寻找其他的解决方案,如JNI或使用第三方库。 JNI允许Java代码直接调用操作系统级别的函数,例如C语言编写的系统库,这些库通常提供了获取硬件信息的...

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

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

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

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

    java获取硬盘序列号

    Java获取硬盘序列号是计算机编程中的一个实用技巧,主要用于识别和追踪特定计算机硬件。硬盘序列号是硬盘制造商分配给每个硬盘的独特标识符,类似于设备的身份证。在某些情况下,例如软件授权验证、设备追踪或者系统...

    java读取硬盘序列号例子

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

    JAVA中获取硬盘序列号源码

    总结来说,Java获取硬盘序列号并非易事,因为它涉及到与操作系统的底层交互。通过JNA库,我们可以方便地调用C语言的API来实现这一功能,但需要注意的是,这可能会导致代码的可移植性降低。在实际开发中,根据项目...

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

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

    查看硬盘序列号的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-...

    硬盘序列号读取diskid

    硬盘序列号通常用于跟踪和支持目的,也是防止非法复制和盗版的重要手段。本文将深入探讨如何使用`diskid`工具来读取硬盘的序列号。 `diskid`是一个命令行实用程序,广泛应用于Linux系统中,用于获取硬盘的详细信息...

    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文件夹下面,合理配置第三方包就可以运行这个程序来获得电脑...

    java 通过jni接口获取当前机器硬盘ID(序列号)

    3. **硬盘ID和序列号**:硬盘ID通常指的是硬盘的序列号,这是一个唯一的数字或字母组合,由硬盘制造商分配给每个硬盘。在Windows系统中,可以使用WMI(Windows Management Instrumentation)或其他系统API来获取硬盘...

    java获取电脑进程信息

    java获取电脑进程信息,java获取电脑进程信息,java获取电脑进程信息。

Global site tag (gtag.js) - Google Analytics