Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a python script from java?

Tags:

java

I have this java application and ran into a requirement to parse an STDF.

Actually all I needed is to get the FAR,MIR and MRR sections of an stdf file. There exists a stdf4j on sourceforge but I can't use it that much due to lack of documentation.

The resolve was to use the stdfparser which is written in python and is relatively more straightforward and gets the job done easier, which in fact I have already modified to suit my needs.

So now I just need to invoke this script verbosely and read the resulting file in Java and move on with the exisiting application.

Problem is when using:

Process p = r.exec("cmd /c python parser.py sample_data.std.gz -v test.txt");

the resulting text file is always blank. (Note that I already ran this on the actual command line and successfully retrieved the contents I needed.)

Is there anything I'm missing out, or are there any better alternatives. (I'm trying to avoid jython as I cannot install it's jar on the production environment.)

like image 1000
lock Avatar asked Oct 29 '10 01:10

lock


1 Answers

You should perhaps call p.waitFor(); so that your Java program doesn't move on until after the external process has finished executing.

Update 1: Also make sure you are reading all the bytes out of p.getErrorStream() and p.getInputStream(). Failure to do so may hang the process.

Update 2: You may also want to check the process return code for an error code. The process return code is accessed via waitFor()'s return value.

like image 181
Mike Clark Avatar answered Oct 20 '22 02:10

Mike Clark