Search code examples
pythonpython-2.7jarsubprocesspopen

Why can't the Python command "subprocess.Popen" find the jar file to run?


I'm trying to run code from this repo: https://github.com/tylin/coco-caption, specifically from https://github.com/tylin/coco-caption/blob/master/pycocoevalcap/tokenizer/ptbtokenizer.py, line 51-52:

p_tokenizer = subprocess.Popen(cmd, cwd=path_to_jar_dirname, \
            stdout=subprocess.PIPE)

The error I get running this is

OSError: [Errno 2] No such file or directory

I can't figure out why the file can't be found.

The jar I'm trying to run is:

stanford-corenlp-3.4.1.jar

You can see the structure of directory by going to https://github.com/tylin/coco-caption/tree/master/pycocoevalcap/tokenizer. For more specificity into what my actual arguments are when I run the line of code:

cmd= ['java', '-cp', 'stanford-corenlp-3.4.1.jar', 'edu.stanford.nlp.process.PTBTokenizer', '-preserveLines', '-lowerCase', 'tmpWS5p0Z'],

and

path_to_dirname =abs_path_to_folder/tokenizer

I can see the jar that needs to be run, and it looks to be in the right place, so why can't python find it. (Note: I'm using python2.7.) And the temporary File 'tmpWS5p0Z' is where it should be.

Edit: I'm using Ubuntu


Solution

  • try an absolute path ( meaning the path beginning from root / )

    https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths

    for relative paths in python see i.e. Relative paths in Python , How to refer to relative paths of resources when working with a code repository in Python

    UPDATE:

    As a test try subprocess.Popen() with the shell=True option and give an absolute path for any involved file, including tmpWS5p0Z

    in this subprocess.Popen() call are involved two paths :

    1) the python path, python has to find the java executable and the stanford-corenlp-3.4.1.jar which is essentially a java program with its own path

    2) the java path of stanford-corenlp-3.4.1.jar

    as this is all too complicated try

    p_tokenizer = subprocess.Popen(['/absolute_path_to/java -cp /absolute_path_to/stanford-corenlp-3.4.1.jar /absolute_path_to/edu.stanford.nlp.process.PTBTokenizer -preserveLines -lowerCase /absolute_path_to/tmpWS5p0Z' ], shell=True)

    Python specify popen working directory via argument

    Python subprocess.Popen() error (No such file or directory)