我想从java执行一个python脚本。代码正在进入 python 文件,但只执行文件的第一行。以下是代码:Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
value = in.readLine();在第一行之后什么都不执行。解决办法是什么?'dir' 值来自final String dir = System.getProperty("user.dir");链接到python文件:https://drive.google.com/file/d/1tvkFTM_Oo5gTS7FyzeNgoeY5DLitFQjD/view?usp=sharing
2 回答

繁星点点滴滴
TA贡献1803条经验 获得超3个赞
问题似乎是,您只是在阅读BufferedReader. 所以改变你的代码如下:
Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
}

holdtom
TA贡献1805条经验 获得超10个赞
当我像这样通过时它工作得很好:
String cmd = "python2.7 "+dir+"/pyfiles/getGitFiles.py "+ownerVal+" "+repoVal+" "+folderVal+" "+branchVal+" "+Values.accessToken;
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);
在'exec'本身内部传递参数会导致问题。
添加回答
举报
0/150
提交
取消