The second command has dropped the " characters from the replacement string. And I don't understand what you are doing with the s at the end, so how about:
sed '/export +JAVA_HOME=\{1,\}JAVA_HOME=/{h;s#=.*#="'$java'"#};$aexport;${x;/^$/{s##export JAVA_HOME="'$java'"'JAVA_HOME="'$java'"#;H};x}' -i ~/.bashrc