I am not a java programmer but it looks like you are lumping options and their arguments into a single command line argument. Separate them.
That is instead of
use
Also you should read/create images in BEFORE operating on them, so place -flatten after the image file name. Also a -quality is an output setting you should do it just before the final argument to make this clear. Finally it is a good idea to specify the background color you want for the flattened image (before flattening).
Yes I know the documentation does not reflect this as it still have some old IM v5 methodology.
See IM examples 'Basics' for a general methodology of how IM processes images.