【问题标题】:Get Motherboard Serial/ID On Linux Machine With Java For Anti-Piracy Usage使用 Java 在 Linux 机器上获取主板序列号/ID 以防盗版
【发布时间】:2013-05-04 06:15:31
【问题描述】:

我正在使用此代码在 windows 中获取主板 ID:

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();
}

在 ubuntu 服务器中尝试此代码会引发异常:

java.io.IOException: Cannot run program "cscript": error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    at java.lang.Runtime.exec(Runtime.java:615)
    at java.lang.Runtime.exec(Runtime.java:448)
    at java.lang.Runtime.exec(Runtime.java:345)
    at tcs.util.MiscUtils.getMotherboardSN(MiscUtils.java:31)
    at tcs.util.Validator.validate(Validator.java:13)
    at test.Shoot.main(Shoot.java:31)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:135)
    at java.lang.ProcessImpl.start(ProcessImpl.java:130)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)

我用谷歌搜索过,但没有找到 linux 的解决方案。我也在这里Printing my Mac's serial number in java using Unix commands 尝试了解决方案,但仍然没有运气。 我知道有一些用 C/C++ 编写的解决方案,但使用 JNI 不是一种选择,因为我们没有这方面的经验并且无法按时完成最后期限。 任何建议将不胜感激,

【问题讨论】:

  • 在一些虚拟机中使用这段代码会发生什么?
  • @BasileStarynkevitch 哪个代码?哪个虚拟机?你能解释一下吗?
  • 您可以在虚拟机(如 VMWare、Qemu 等)内运行任何操作系统(包括 Windows)
  • 试试dmidecode,但不要以为你在保护你的软件
  • “我用谷歌搜索过,但没有找到适用于 linux 的解决方案” 没关系。就反盗版而言,您也没有真正找到适用于 Windows 的解决方案。

标签: java linux jakarta-ee web-applications ubuntu


【解决方案1】:

使用带有绝对路径的“cscript”命令

FULL_PATH/cscript //NoLogo " + file.getPath())

示例:/home/myprojext/cscript //NoLogo " + file.getPath())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 2016-06-12
    • 2010-10-28
    • 2017-09-14
    • 2011-01-30
    • 2011-06-26
    • 2011-05-12
    相关资源
    最近更新 更多