0

I know that there is a lot of similar questions, but neither solved my problem...

So, I would like to execute python script from java using ProcessBuilder...

Here is a method which I wrote and which does not work:

public void stemPosts(String scriptPath, String inputFile, String outputFile)
        throws IOException {
    ProcessBuilder process = new ProcessBuilder("python", scriptPath,
            inputFile, outputFile);
    process.start();
}

And here is method call (<user_path> is just to hidden personal info):

dataManager.stemPosts(
            "D:/<user_path>/stemmer/Croatian_stemmer.py",
            "D:/<user_path>/stemmer/posts.txt",
            "D:/<user_path>/stemmer/stemmedPosts.txt");

First parameter is script, second parameter is first script parameter (inputFile) and third parameter is second script parameter (outputFile)...

Execution in cmd is simple: python Croatian_stemmer.py posts.txt stemmedPosts.txt and that works...

Code above only creates output file but it does not fill it with data...

I tried changing file separators and it did not help...

mister11
  • 82
  • 2
  • 7

1 Answers1

1

For starters I would look at what output you see from the execution of the script by redirecting your ProcessBuilder output which is described here.

My hunch would be it's something to do with PYTHONPATH or an error in the script but without seeing either the script or the output of the execution it's impossible to know.

Community
  • 1
  • 1
Corbell
  • 1,283
  • 8
  • 15
  • so, I redirected it as you suggested, but there is nothing printed out; next, python path is set correctly; and here is script in its original form: http://pastebin.com/0PNQrjZx – mister11 May 10 '14 at 23:34
  • 1
    I think I see it - your python script is assuming that 'rules.txt' and 'transformations.txt' are in the current working directory. If you run your script from the terminal from somewhere *besides* the stemmer directory you'll likely see the same problem. The JVM working directory is not going to be inside stemmer when it runs the script. Have your python script figure out its own absolute path and reference rules.txt and transformations.txt based on that - see http://stackoverflow.com/questions/247770/retrieving-python-module-path – Corbell May 10 '14 at 23:44
  • One more note you can also probably just use sys.argv[0] – Corbell May 10 '14 at 23:46
  • Thanks for noticing current working directory problem... That's the thing that I missed...As I'm not really good with python (this is not my script) so I solved problem in Java by setting `ProcessBuilder#directory` to the working directory and now everything works as charm :) Thanks for your help :) – mister11 May 10 '14 at 23:54
  • Cool! Glad to help. Feel free to check off / vote up my answer. :) – Corbell May 11 '14 at 01:46