Better java opts handling

See commit for details.
Closes #518