CGI and JAVA are fundamentally dissimilar, and for most applications are NOT interchangable.
CGI is a protocol for running programs on a WWW server. Whilst JAVA can also be used for that and even has a standardised API (the servlet, which is indeed an alternative to CGI), the main role of JAVA on the Web is for client side programming (the applet).