如何从java调用和执行python类方法。我当前的java代码可以工作,但前提是我要编写:if __name__ == '__main__':
print("hello")
但是我想执行一个类方法,不管if __name__ == '__main__':
我要运行的示例python类方法:
^{pr2}$
基本上我想逃跑SECFileScraper.tester\u func()在java中。
我的Java代码:try {
ProcessBuilder pb = new ProcessBuilder(Arrays.asList(
"python", pdfFileScraper));
Process p = pb.start();
BufferedReader bfr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
System.out.println("Running Python starts: " + line);
int exitCode = p.waitFor();
System.out.println("Exit Code : " + exitCode);
line = bfr.readLine();
System.out.println("First Line: " + line);
while ((line = bfr.readLine()) != null) {
System.out.println("Python Output: " + line);
}
} catch (Exception e) {
e.printStackTrace();
}
pdfFileScraper是python脚本的文件路径。在
我尝试过jython,但是我的python文件使用pandas和sqlite3,这两个不能用jython实现。在