java - 如何在 java 中运行这个 python 脚本?
问题描述
我想在我的 java 应用程序中使用这个脚本:https ://github.com/jcapona/amazon-wishlist-scraper 。
我环顾四周,并尝试像这样执行脚本:
public static void main(String[] args) throws IOException {
String s = null;
Process p = Runtime.getRuntime().exec("python C:\\\\Users\\\\Home\\\\work\\\\test.py");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while ((s=stdInput.readLine()) != null) {
System.out.println(s);
}
}
但我没有得到任何输出。我需要什么才能运行这个特定的脚本?
解决方案
你可以这样使用:
String command = "python /c start python path\to\script\script.py>";
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line;
while ((line = stdInput.readLine()) != null) {
System.out.println(line);
}
stdInput.close();
while ((line = stdErr.readLine()) != null) {
System.out.println(line);
}
stdErr.close();
p.waitFor();
System.out.println("Done.");
p.destroy();
Protip:始终从文件资源管理器选项卡中复制您的路径
并且由于您收到 JSON 响应,请尝试使用 GSON 库对其进行解析。
如果您想使用 java 在 python 上大量工作,请尝试探索Jython
推荐阅读
- java - java.lang.NoSuchMethodException 使用 Pagefactory 对象运行 Appium Java 类时出错
- javascript - 如何在后端验证来自前端的输入 - MERN
- php - 如何为字符串base64 blob不可读创建链接
- python - 在 Pandas 中加入字符串列并删除重复项和其他列
- sqlite - 如何在 Peewee 和 SQLite 的 FTS5 中使用 trigram tokenizer/similarity 选项?
- c++ - 无法显示图像opencv(c ++)
- authentication - JWT 注册问题
- javascript - db.collection 不是函数 firebase firestore
- python - 为什么一个简单的网页抓取代码在 pycharm 上运行缓慢但在 google colab 上运行得很快?
- javascript - 如何使用正则表达式获取JavaScript 标记中的所有 URL src?