1

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

april
  • 131
  • 3
  • 11
  • 1
    I'd say this issue might be solved by flushing your streams. E.g., assuming both Java and Python write to the standard output (not standard error), you can try writing `print("mvn clean", flush=True)` - Disclaimer: I only tested this with Python3 while you seem to rely on Python2 syntax. – ErikMD May 26 '18 at 14:07
  • 1
    According to this [SO answer](https://stackoverflow.com/a/230774/9164010), one can do `sys.stdout.flush()` to flush stdout in Python2… – ErikMD May 26 '18 at 14:10
  • Two unrelated remarks: (*i*) maybe you should use `subprocess.check_call` if you want to raise an exception when the run command fails; (*ii*) I guess the `export MAVEN_OPTS` command is run in a subshell, so the last shell command you run in you Python script won't be aware of the `MAVEN_OPTS` variable introduced by the last-but-one. – ErikMD May 26 '18 at 14:18

0 Answers0