public void analyzeOwlByPython(String owlPath) {
MdtLogConsole console = MdtLogConsoleFactory.getInstance().getConsole();
if (StringUtil.isEmpty(filename)) {
setMessage("文件名不能为空,请填写文件名。", IMessageProvider.ERROR);
return;
}
File outputFile = new File(projectPath + owlqxPath);
if (outputFile.exists()) {
outputFile.delete(); // 删除以showName命名的文件
}
String outputFilePath = projectPath + "\\优化流程模型文件\\" + filename + ".txt";
URL url = Activator.getDefault().getBundle().getResource("resources");
String resourcesPath = null;
try {
resourcesPath = FileLocator.toFileURL(url).getPath();
} catch (IOException e1) {
e1.printStackTrace();
}
File resourcesDir = new File(resourcesPath + "sarva/owlQX.py");
String pathc = System.getenv("APPDATA").replaceAll("\\\\", "/") + "/sarva/owlQX.py";//python脚本路径
console.print("数据清洗文件c" + pathc);
String pyPath = resourcesDir.getPath().replaceAll("\\\\", "/");
console.print("数据清洗文件r" + pyPath);
String[] cmdArr = new String[] {"python",pyPath, projectPath + dataPath, outputFilePath};
Process process;
try {
process = Runtime.getRuntime().exec(cmdArr);
process.waitFor();
System.out.println();
StringBuilder content = new StringBuilder(pyPath);
try (BufferedReader reader = new BufferedReader(new FileReader(outputFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
styledText.setText(content.toString());
}
ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (Exception e) {
e.printStackTrace();
}
}
通过以上代码可以实现在java程序中调用python脚本的功能