0
我使用這個代碼來獲取主板ID在Windows中:獲取主板序列號/ ID在Linux機器的Java對於反盜版使用
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)
我已經google了一下,但沒有發現Linux的解決方案。我也試過這裏的解決方案Printing my Mac's serial number in java using Unix commands,仍然沒有運氣。 我知道有用C/C++編寫的解決方案,但使用JNI不是一種選擇,因爲我們沒有經驗,並且無法按時完成截止日期。 任何建議將被理解的,以絕對路徑
在某些VM內使用此代碼會發生什麼情況? – 2013-05-04 06:26:22
@BasileStarynkevitch哪個代碼?哪個VM?你能解釋一些嗎? – MoienGK 2013-05-04 06:27:47
你可以在虛擬機內運行任何操作系統(包括Windows)(例如VMWare,Qemu,...) – 2013-05-04 06:29:00