5

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

1
  • in windows environment, the path to the executable should be explicitly declared such that: Process p = r.exec("C:/WINDOWS/system32/cmd.exe /c python parser.py sample_data.std.gz > test.txt"); Commented Oct 29, 2010 at 6:49

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

its actually the next line after that code, i just didn't include it to avoid confusion but i guess it did otherwise :P
OK, next make sure you are draining all the bytes out of p.getErrorStream() and p.getInputStream(). Also, is it at all possible that using "cmd /c python" instead of just running "python" directly, might cause a problem?
2

In addition to making sure you call p.waitFor(), you may also have to read the contents of the streams. If the streams aren't read, the processes will stall.

Try adding this class: http://www.physionet.org/physiotools/puka/sourceCode/puka/StreamGobbler.java

And calling it:

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

You might be able to come up with a more efficient class on your own, but that one should at least give you the basic idea (note that it wasn't written by me).

Comments

0

Try creating a cmd file and then call that using Java.

1 Comment

There are better ways of doing this. Refer to Mike Clark's answer~_~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.