Home » Java » Print terminal output to file when calling java from python

Print terminal output to file when calling java from python

Posted by: admin July 12, 2018 Leave a comment

Questions:

I am running Java code (using maven) from python.

    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)

Unlike command line, when I try to out put the execution in an external file like out.txt, it first prints out the Java output and then the python output.

First Java output

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

Then python output

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

but when I don’t try to print the terminal output in an external file, in the terminal I see it prints out based on the order of what is in my code:

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:

Why is this happening

Answers: