title: "cannot find the main class" error calling a java program using subprocess.Popen, whereas the same call just works using os.system
body: <p>I am writing a script that calls another java program, to perform some tasks. It is very important that each task starts, if and only if, the previous one has finished. Each task might run for several days, producing a huge amount of output. It works on a huge database</p>
  
  <p>I noticed that if I run the script using os.system(), it runs for several minutes the first task and then for some reason starts the second one, although the first task is not complete. The first task doesn't end normally it's like it suddenly stopped working. If i try it on a smaller database which it takes only few minutes to process, everything works fine.</p>
  
  <p>I don't understand why it behaves this way!</p>
  
  <p>For this reason I was trying to use Popen() so I can use the communicate() in order to make wait till the end of the process called.</p>
  
  <p>For some reason the same command to call the external java program works with os.system(), while it says that it cannot find the main() with Popen(). I am using relative paths, I tried absolutes path too with no luck.</p>
  
  <p>Is there an alternative to the Popen()? However, I don't understand why it cannot find the main class. What is exactly the difference between os.system() and Popen() when calling an external program? How can I solve or workaround my problem? </p>
  
  <p>here is the code:</p>
  
  <pre><code>import os
  from subprocess import Popen
  
  def doWork():
  
      owd = os.getcwd()
      cmd = "java -Dfile.encoding=Cp1252 -classpath \"bin;lib\\mysql-connector-java-5.0.8-bin.jar;lib\\sqlitejdbc-v056.jar\" core.ODBManager"
      os.chdir("ODBManager")
  
      #this way does not work
      p = Popen(cmd + "1", shell=True) #import
      p.communicate() #now wait
  
      p = Popen(cmd + "2", shell=True) #convert
      p.communicate() #now wait
  
      p = Popen(cmd + "3", shell=True) #export
      p.communicate() #now wait
  
      #this way works but does not wait the command to finish
      #before starting another one
  #   os.system(cmd+" 1") #import
  #   os.system(cmd+" 2") #convert
  #   os.system(cmd+" 3") #export
  
      os.chdir(owd)
  </code></pre>
  
  <p>thank you. If something is not clear, feel free to ask.
  P.S. the java program (build with eclipse) is in a subfolder and it reads some input from a file which is in another subfolder so I had to go with chdir, otherwise couldn't find that file.</p>