Fix resource leaks in Java bindings