Installing Java from a package
To install Java, you must compile it or install a prebuilt package. Currently there are no pre-built packages for the FreeBSD 5.x tree, although you can use the older Java 1.3.1 packages for FreeBSD 4.8 (and later,) tree. These are older packages (the latest Java is 1.4.2 (although 1.5 is just around the corner, widespread use of this recent release is not yet common).
If you're using FreeBSD 4.x, and you don't mind using an older Java, check out the FreeBSD Foundation's Java pre-built binaries. They're .tbz's and you'll want to download them to /usr/ports/distfiles and then go into the /usr/ports/java/diablo-jdk (or /usr/ports/java/diablo-jre etc) directory and run
from there so that the port builds properly.
If you want to use a newer Java, or if you're running the 5.x releases instead of the 4.x tree, you'll need to compile and install your own Java VM.
 Java 1.5
Java 1.5 has been released as binary packages for x86. http://www.freebsdfoundation.org/downloads/java.shtml
 But I don't want Java 1.3.1, or I am running FreeBSD 5.x!
Sorry, you have to build it yourself. In order to built it yourself, you have to have a working Java first. So how do you do this? You install linux emulation, use the linux Java in ports, download the Java sources distributed by Sun under their SCSL scheme and build it. See instructions below.
 Installing Java 1.4.2 SDK, the quick and dirty way.
NOTE THAT THERE ARE LICENSING ISSUES WITH JAVA TECHNOLOGIES FROM SUN.COM AND FreeBSD -- IT IS YOUR RESPONSIBILITY TO RECONCILE THESE AS THEY PERTAIN TO YOUR USE OF JAVA
You will need:
1. about 2 gigs free disk space. Check that you have this by running df
3. to download the following files:
bsd-jdk14-patches-6.tar.gz from Eyesbeyond.com j2re-1_4_2_08-linux-i586.bin from Sun's j2se JRE page j2sdk-1_4_2-bin-scsl.zip from This page at sun.com j2sdk-1_4_2-mozilla_headers-unix.zip from This page at sun.com j2sdk-1_4_2-src-scsl.zip from This page at sun.com j2sdk-1_4_2_06-linux-i586.bin from Sun's j2se JDK page
For all the sun.com links, you'll need to login (and accept their EULA,) in order to download the files. It's free and relatively painless. If you don't agree to Sun's terms, you don't get to download the files and therefore don't get to build your own JRE/JDK/JVM. Deal with it.
4. to enable linux compatibility -- you can do this via sysinstall if you didn't do it on your initial install.
5. add the following line to your /etc/fstab:
linprocfs /compat/linux/proc linprocfs rw 0 0
6. to run linprocfs -- you may need to do this:
samizdata# kldload linprocfs samizdata# mount /compat/linux/proc
7. place the files you downloaded in the third step in /usr/ports/distfiles and unzip any that are .zip but leave the .bin and .tar.gz files alone.
8. go into the jdk14 dir:
samizdata# cd /usr/ports/java/jdk14
9. and install it:
samizdata# make install
10. wait. It takes a very long time. Go catch a movie or something.
Note that you don't want to make install clean because if something fails at some point during the install, the clean will wipe out your work files. Just run make install and when it's done, clean up with a make clean && make distclean after it's installed. Don't forget to use rehash after the install, and you can test to see that it's installed and working by running a java -version.
See http://taosecurity.blogspot.com/2004/11/installing-java-on-freebsd-in-january.html for a similar method of doing this.
See also the discussion at Talk:Java
do you know that SUN has changed the site and that previous listed links are not more working?