从python调用java时将终端输出打印到文件

2024-06-26 14:44:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从python运行Java代码(使用maven)。你知道吗

    with changeDir(runDir):
        print "mvn clean"
        subprocess.call(["mvn clean"], shell=True)
    with changeDir(runDir):
        print "mvn compile"
        subprocess.call(["mvn compile"], shell=True)
    print "compile time",datetime.now() - startTime     
    with changeDir(runDir):
        print "mvn package"
        subprocess.call(["mvn package -DskipTests=true"], shell=True)
    with changeDir(runDir):
        subprocess.call(["export MAVEN_OPTS=\"-Xmx10g -Xms10g -XX:MaxPermSize=10g -XX:+UseConcMarkSweepGC -XX:-UseGCOverheadLimit\""], shell=True)
        print "mvn exec"
        subprocess.call(["mvn exec:java -Dexec.mainClass=examples.Example -Dexec.args=\"pathToFile"+fname+"\""], shell=True)

与命令行不同,当我尝试在外部文件中执行时顺序文件,它首先输出Java输出,然后输出python输出。你知道吗

第一个Java输出

[INFO] Scanning for projects...
[INFO] Inspecting build with total of 1 modules...
[INFO] Installing Nexus Staging features:
....
..

然后是python输出

mvn clean
mvn compile
compile time 0:00:08.027680
mvn package
mvn exec

但是,当我不尝试在外部文件中打印终端输出时,在终端中,我看到它根据代码中的顺序打印出来:

mvn clean
    [INFO] Scanning for projects...
    [INFO] Inspecting build with total of 1 modules...
    [INFO] Installing Nexus Staging features:
....
..
mvn compile
    [INFO] Scanning for projects...
    [INFO] Inspecting build with total of 1 modules...
    [INFO] Installing Nexus Staging features:

为什么会这样


Tags: infocleantruepackagewithjavashellcall