How do I know if a software is done writing a file if I am executing that software from java?For example, I am executing geniatagger.exe with an input file RawText that will produce an output file TAGGEDTEXT.txt. When geniatagger.exe is finished writing the TAGGEDTEXT.txt file, I can do some other staffs with this file. The problem is- how can I know that geniatagger is finished writing the text file?
try{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("geniatagger.exe -i "+ RawText+ " -o TAGGEDTEXT.txt");
}
You can’t, or at least not reliably.
In this particular case your best bet is to watch the Process complete.
You get the process’ return code as a bonus, this could tell you if an error occurred.
If you are actually talking about this GENIA tagger, below is a practical example which demonstrates various topics (see explanation about numbered comments beneath the code). The code was tested with v1.0 for Linux and demonstrates how to safely run a process which expects both input and output stream piping to work correctly.
Use a
ProcessBuilderto start your process, it has a better interface than plain-oldRuntime.getRuntime().exec(...).Set up stream piping in different threads, otherwhise the
waitFor()call in ({5}) might never complete.Note that I piped a
FileInputStreamto the process. According to the afore-mentioned GENIA page, this command expects actual input instead of a-iparameter. TheOutputStreamwhich connects to the process must be closed, otherwhise the program will keep running!Copy the result of the process to a
FileOutputStream, the result file your are waiting for.Let the main thread wait until the process completes.
Clean up all streams.