How to run python script from java?

I have this java application and I came across the requirement of parsing STDF.

In fact, all I need is to get the FAR, MIR, and MRR sections of the stdf file. There is stdf4j on sourceforge, but I can not use it so much due to lack of documentation.

It was decided to use stdfparser, written in python, and is relatively simple and easy, which in fact I have already changed according to my needs.

So, now I just need to repeatedly call this script and read the resulting file in Java and move on to the existing application.

The problem is to use:

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

the resulting text file is always empty. (Note that I have already done this on a real command line and successfully retrieved the desired content.)

Is there something I'm missing out on, or are there any better alternatives. (I am trying to avoid jython since I cannot install its jar in a production environment.)

+5
source share
3 answers

You might need to call p.waitFor();so your Java program does not move until the external process completes.

Update 1: Also make sure that you read all bytes from p.getErrorStream()and p.getInputStream(). Failure to do so may cause the process to freeze.

Update 2: You can also check the process return code for the error code. The reverse process code is accessed through the return value waitFor().

+6
source

, p.waitFor(), . , .

: http://www.physionet.org/physiotools/puka/sourceCode/puka/StreamGobbler.java

:

 new StreamGobbler(p.getErrorStream(), "Error");
 new StreamGobbler(p.getInputStream(), "Input");

, , ( , ).

+2

Try creating a cmd file, and then call it using Java.

0
source

All Articles