我有一个Python脚本,我试图通过Java代码运行它。
当通过我的Ubuntu虚拟机上的Linux终端命令运行Python脚本时,使用的命令与通过Java脚本传递的命令相同。
当运行另一个Python脚本时,Java代码运行得很好,该脚本的运行速度比我尝试运行的Python脚本快。
但是,尽管Python脚本和Java脚本都运行得很好,但不知何故,当我将两者放在一起时,什么也没有发生:.txt文件没有更新,所以Java脚本打印出它包含的任何旧值。
System.out.println("starting...");
try {
Process process = Runtime.getRuntime().exec("python3 /home/.../PycharmProjects/.../fraudanalysis.py abc def");
Thread.sleep(900000);
# Or try System.out.println(process.waitFor());
File file = new File("/home/.../PycharmProjects/.../output.txt");
Scanner newLineReader = new Scanner(file);
System.out.println(newLineReader.nextLine());
} catch(Exception e) {
System.out.println(e);
}上面的代码应该使用两个参数在提供的绝对目录中运行Python3脚本。Python3脚本在大约13分钟后完成,并更新output.txt文件,等待15分钟后由Python3程序读取该文件(或者您可以告诉线程等待完成-- process.WaitFor()返回1)。
def testScript():
time.sleep(780)
return_string1 = sys.argv[1]
return_string2 = sys.argv[2]
outputFile = open(os.path.dirname(os.path.abspath(__file__)) + "/output/output.txt", "w+")
outputFile.write(return_string1 + " " + return_string2)
print("Python run complete")
if __name__ == "__main__":
testScript()上面的脚本是Python脚本的一个很好的替身。如果您将Python脚本的睡眠时间降低到10分钟,那么当Java发送命令时,它就会运行。但是,在上面显示的睡眠时间,Java显然无法运行脚本,或者脚本运行尝试以失败告终。
附加信息: Java命令是使用JavaFX按钮激活的。Java脚本是用IntelliJ IDEA开发的,Python脚本是用PyCharm创建的。
我的问题是,当两个脚本都独立工作时,导致这个问题的可能原因是什么?
发布于 2020-08-21 12:29:18
我做了一个小小的修改就能让它正常工作。我使用了相对文件位置和TimeUnit.MINUTES.sleep(15);
package org.openjfx;
import java.io.File;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class TestWait {
public static void main(String[] args) {
System.out.println("starting...");
String dir="src/main/resources/org/openjfx/";//location of the python script
try {
System.out.println("Working Directory = " + System.getProperty("user.dir"));
//System.out.println("python3 " + dir+"fraudanalysis.py abc def");
Process process = Runtime.getRuntime().exec("python3 " + dir+"fraudanalysis.py abc def");
System.out.println(process.waitFor());
TimeUnit.MINUTES.sleep(15);
File file = new File("src/main/resources/org/openjfx/output.txt");
Scanner newLineReader = new Scanner(file);
System.out.println(newLineReader.nextLine());
} catch (Exception e) {
System.out.println(e);
}
}
}这是我使用的python。
import sys
import time
def testScript():
return_string1 = sys.argv[1]
return_string2 = sys.argv[2]
time.sleep(780)
outputFile = open("src/main/resources/org/openjfx/output.txt", "w+")
outputFile.write(return_string1 + " " + return_string2)
print("Python run complete")
if __name__ == "__main__":
testScript()发布于 2020-08-20 07:51:37
作为一个简单的建议,您不应该依赖具有固定参数的Thread.sleep方法,例如15分钟。您的数据可能会变大或变小,这种处理方式效率不高。
您可以尝试调用Process.waitFor()方法,以便在python进程结束时,线程继续运行。
此外,您可以尝试使用ProcessBuilder,在遇到系统执行错误的情况时,它有时会有所帮助。
下面是一些代码。在sub()中,您不能更改python程序,但是要使sub2()正常工作,您必须修改python程序,使其输出为标准输出,并且Java将执行到output.txt文件的重定向。
public void sub() {
System.out.println("startig...");
Scanner newLineReader = null;
try {
Process process = Runtime.getRuntime().exec("python3 /home/.../PycharmProjects/.../fraudanalysis.py /home/.../PycharmProjects/.../fraudAnalysis.db 500");
process.waitFor();
File file = new File("/home/.../PycharmProjects/.../output.txt");
newLineReader = new Scanner(file);
String line;
while((line=newLineReader.nextLine())!=null) {
System.out.println(line);
}
} catch(IOException ioe) {
ioe.printStackTrace();
}catch(InterruptedException ie) {
ie.printStackTrace();
}finally {
newLineReader.close();
}
}
public void sub2() {
ProcessBuilder pb =
new ProcessBuilder("python3",
"/home/.../PycharmProjects/.../fraudanalysis.py",
"/home/.../PycharmProjects/.../fraudAnalysis.db", "500");
File log = new File("/home/.../PycharmProjects/.../output.txt");
pb.redirectErrorStream(true);
pb.redirectOutput(Redirect.appendTo(log));
Process p = null;
try {
p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
Scanner newLineReader = null;
try{
newLineReader = new Scanner(log);
String line;
while((line=newLineReader.nextLine())!=null) {
System.out.println(line);
}
}catch(IOException ioe) {
ioe.printStackTrace();
}
}发布于 2020-08-21 07:18:59
这是超时错误。无法修复。只需在Java和Python之间选择,然后用它编写所有内容。没有理由同时使用两者。
https://stackoverflow.com/questions/63496238
复制相似问题