Javaflatten terms

Fixing the flattenTerms in java backend.