java.io.IOException:无法运行程序";“WMIC”:CreateProcess错误=2,系统找不到指定的文件
我想使用java代码获取Windows序列号,但遇到异常。 我的java代码在某些版本的windows(7,8)上工作,而在windows 10 pro上不工作,并对同一代码抛出异常(在windows vista上未验证)java.io.IOException:无法运行程序";“WMIC”:CreateProcess错误=2,系统找不到指定的文件,java,Java,我想使用java代码获取Windows序列号,但遇到异常。 我的java代码在某些版本的windows(7,8)上工作,而在windows 10 pro上不工作,并对同一代码抛出异常(在windows vista上未验证) 我遇到了同样的问题。我通过在启动进程之前显式指定进程必须运行的目录来解决此问题: List<String> cmd = new ArrayList<String>() {{ add("WMIC.exe"); add("diskdrive"); ad
我遇到了同样的问题。我通过在启动进程之前显式指定进程必须运行的目录来解决此问题:
List<String> cmd = new ArrayList<String>() {{
add("WMIC.exe"); add("diskdrive"); add("get"); add("serialNumber");
}};
ProcessBuilder pb = new ProcessBuilder().directory(getExecDir());
Process p = pb.command(cmd).start();
// Other code here
错误或异常是什么?感谢回复,java.io.IOException:无法运行程序“WMIC”:CreateProcess error=2,系统找不到指定的文件。您是否安装了WMIC命令行实用程序?是否检查WMIC是否在您的路径上?感谢回复。是的,我添加了“%SystemRoot%\System32\wben”路径并重新启动了电脑。现在它工作正常。
List<String> cmd = new ArrayList<String>() {{
add("WMIC.exe"); add("diskdrive"); add("get"); add("serialNumber");
}};
ProcessBuilder pb = new ProcessBuilder().directory(getExecDir());
Process p = pb.command(cmd).start();
// Other code here
/**
* Get the execution directory for WMIC
* @return File object pointing to the execution directory
* @throws IOException Execution directory doesn't exist
*/
private static File getExecDir() throws IOException {
String root = System.getenv("SystemRoot");
File dir = new File(root, "System32" + File.separatorChar + "wbem");
if (!dir.exists() || !dir.isDirectory()) {
throw new IOException('"' + dir.getAbsolutePath() + "\" does not exist or is not a directory!");
}
return dir;
}