开发者

"cannot find the main class" error calling a java program using subprocess.Popen, whereas the same call just works using os.system

开发者 https://www.devze.com 2023-03-31 19:03 出处:网络
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

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

I noticed that if I run the script using os.system(),开发者_如何学Go 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.

I don't understand why it behaves this way!

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.

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.

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?

here is the 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)

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.


Not sure if this is a typo, but in the way "that does not work" as you describe it, you have no space between the cmd and the concatenated "1", "2" and "3" strings. With the os.system calls you do have the space. Without the space you are effectively changing the name of the class whose main method you want to execute and therefore you get the error reported.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号