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...

1 Answer 1

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.

5
  • 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: pastebin.com/0PNQrjZx
    – mister11
    Commented May 10, 2014 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 stackoverflow.com/questions/247770/…
    – Corbell
    Commented May 10, 2014 at 23:44
  • One more note you can also probably just use sys.argv[0]
    – Corbell
    Commented May 10, 2014 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
    Commented May 10, 2014 at 23:54
  • Cool! Glad to help. Feel free to check off / vote up my answer. :)
    – Corbell
    Commented May 11, 2014 at 1:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.