java - 从 JAR 文件中执行 python 脚本
问题描述
我正在尝试编写一个执行 python 脚本的 Java 应用程序,以便将值返回给原始程序。但是,脚本是用 Python3 编写的,所以我不能使用 Jython。
我当前的程序在 Intellij 中运行良好,但是当我导出到 JAR 文件时它不再运行(我假设是因为文件路径不同)。我知道过去曾提出过类似的问题,但似乎没有一个解决方案有效。
String currentPath = new File("").getAbsolutePath();
String path = Paths.get(currentPath, "src", "com", "engine", "pythonScript.py").toUri().toString();
String[] cmd = new String[]{"python", path, data};
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String output;
while ((output = stdInput.readLine()) != null) {
System.out.println(output);
if (checkOutput(output)) {
return output;
}
}
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
while ((s=error.readLine()) != null) {
System.out.println(s);
}
} catch (IOException e) {
e.printStackTrace();
}
解决方案
推荐阅读
- julia - 朱莉娅 | 在二维数组中推送或追加
- python - 无法使用 Flask、MySQL 更新记录
- c# - 以编程方式从 Azure 订阅中删除用户 (C#)
- sharepoint - SharePoint 2013 列表 - 列索引、列表阈值限制、REST api
- sql - Access 2010 SQL 查询选择带有临时表的内部联接
- c# - 如何在 PuppeteerSharp 中等待异步函数评估
- php - Doctrine - 在 From 中进行子选择
- python - 浮点错误的发生以及如何防止它们
- javascript - React Native 中的 Promise
- google-app-engine - 如何在本地谷歌应用引擎环境中创建拉/推队列