使用Java在Linux机器上获取主板序列号/ID,用于反盗版
我正在使用此代码在windows中获取主板ID:使用Java在Linux机器上获取主板序列号/ID,用于反盗版,java,linux,jakarta-ee,web-applications,ubuntu,Java,Linux,Jakarta Ee,Web Applications,Ubuntu,我正在使用此代码在windows中获取主板ID: public static String getMotherboardSN() { String result = ""; try { File file = File.createTempFile("realhowto", ".vbs"); file.deleteOnExit(); FileWriter fw = new java.io.FileWriter(file);
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)
java.io.IOException:无法运行程序“cscript”:错误=2,没有这样的文件或目录
位于java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
位于java.lang.Runtime.exec(Runtime.java:615)
位于java.lang.Runtime.exec(Runtime.java:448)
位于java.lang.Runtime.exec(Runtime.java:345)
在tcs.util.MiscUtils.getMotherboardSN(MiscUtils.java:31)
位于tcs.util.Validator.validate(Validator.java:13)
at test.Shoot.main(Shoot.java:31)
原因:java.io.IOException:error=2,没有这样的文件或目录
位于java.lang.UNIXProcess.forkAndExec(本机方法)
位于java.lang.UNIXProcess(UNIXProcess.java:135)
在java.lang.ProcessImpl.start(ProcessImpl.java:130)
位于java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
我在谷歌上搜索过,但没有找到linux的解决方案。我在这里也尝试过解决方案,但仍然没有成功。
我知道有些解决方案是用C/C++编写的,但使用JNI不是一个选项,因为我们在这方面没有经验,也不能按时完成最后期限。
如有任何建议,请使用带有绝对路径的“cscript”命令
完整路径/cscript//NoLogo“+file.getPath())
示例:/home/myprojext/cscript//NoLogo“+file.getPath())在某些VM中使用此代码时会发生什么情况?@basilestrynkevitch哪个代码?哪个虚拟机?你可以在虚拟机(如VMWare、Qemu等)内运行任何操作系统(包括Windows)。尝试
dmidecode
,但不要认为你在保护你的软件“我在谷歌上搜索过,但没有找到linux的解决方案”没关系。就反盗版而言,你也没有真正针对Windows的解决方案。