JAVA代码中如何调用python脚本
  WYub0BgMG5Ci 2023年12月08日 100 0
	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脚本的功能
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月08日 0

暂无评论

推荐阅读
WYub0BgMG5Ci