CommandLine instanceMain method does holds index file open
I am writing a Java program to a call a number of CommandLine classes in succession to sort, reorder, index
I call SortSam, CleanSam,AddOrReplaceReadGroups, ReorderSam. Each call has a main which calls System.exit. Instead if calling that main (which would kill my pipeline, I call instanceMain on the relevant class using reflection. I want to pass as output file tmp.bam and expect tmp.bai to be generated as an index. That works.
After I call invokeMain I want to rename the tmp file and the tmp index to the original names. I first rename the original to get it out of the way and also the original index file .
My problem is that the original index file has not been closed and cannot be moved out of the way.
While it is true that calling System.exit (as the CommandLine main does) addresses this issue - this is a VERY BLUNT HAMMER and precludes writing a program which chains command line operations.
How can I close the index file without calling System.exit