These instructions will assume that you are installing the JDK1.6.0_21. It is recommended to leave the version number in the install directory (e.g. 1.6.0_21). This will allow you to install several versions of the jdk under the same root directory (e.g. C:\java), and to easily recognize which version of the JDK lies in which directory.
You have to choose whether you want to use a zipped (.tar.gz) file or use an RPM package. Older versions of Java (up to Java 6) used a self-extracting binary file rather than a compressed file. If your system is RPM-based, you may wish to use the RPM package instead of the binary file.
Java should already be installed in your system, but the JDK used should be Open JDK, not Oracle JDK. If you wish to use the Cracle JDK, you will have to install it exactly the same way you would for any other flavour of Linux.
Installation instructions for Mac OS will be coming soon...
Tip: In the JDK install directory, there should be a file called src.zip. It contains all sources of the Java API. You might be interested to have a look at them someday !
You will probably have to open a new command line or terminal window to use the updated PATH; the old window usually uses the old PATH environment variable.
Windows Vista / Windows 7
The directions vary depending which shell your computer uses, but most people use the "bash" shell. This is how to alter a PATH variable on "bash". Note the PATH variable already exists, so it is only necessary to add the PATH to one's Java installation.
If you prefer, you can set a new temporary PATH by writing an instruction similar to this at a terminal: "export PATH=/usr/java/jdk1.8.0_05/bin:$PATH". That will update your PATH for that instance of the terminal, so you can use a different JDK from normal.
Instructions for Mac OS coming soon...
The JAVA_HOME variable is used by other programs to know where your JDK is. Most IDEs will use it. It is also used as a convenient shortcut, to avoid having to change the other places referring to the JDK install directory. For example, we have used it to set the PATH variable (%JAVA_HOME%). This is very convenient, because if you install another version of the JDK, all you need to do is to update your JAVA_HOME, without having to touch the PATH.
But don't set JAVA_HOME as a user variable and use it in a system PATH (see discussion here).
The PATH variable is used by your system command prompt. When you execute a program, your system will look for its location. It will scan all directories included in the PATH, and look for the program you are executing. This means that when you type "java", or "javac" at the command prompt, your system will look into %JAVA_HOME%\bin", find the command and execute it. If you don't set your PATH properly, you'll end up with an error message like "Command not found".
Open the command prompt, go to a directory different from your JDK install directory, and type "javac -version". This should display the version of your SDK. Make sure it is correct. If an error like "Command not found" is displayed, make sure you have correctly set your PATH. Check the environment variables settings again. If the version displayed is not the same as the JDK you have installed, another installed JDK may conflict. Make sure you have set your JDK directory at the beginning of the PATH variable.
This is where the fun begins ! Let's create a simple greetings class. Use your favorite text editor to create a new file. You don't need any advanced IDE like Eclipse or IntelliJ yet. For the moment, stick to the editor you're used to. Copy/paste the following code in your editor :
Save the file in the directory of your choice. But do not save it under the JDK install directory. Instead, save it in a directory where you'd like to keep your java sources in. For example, C:\java\src. Name the file MooseGreetings.java. Be extra careful with the uppercase and lowercase. The filename must match exactly the name of your class. mOoSegReeTings.java won't work. Also be careful that your editor does not append the ".txt" extension to your file. Some filthy editors like Windows Notepad may do it, but Notepad is probably not your favorite editor !
(For Mac-specific instructions, go to http://www.coderanch.com/t/111632/Mac/JAVA-MAC-OS)
At the command prompt, go to the directory where you have saved the MooseGreetings.java file. Type "javac MooseGreetings.java", and press the enter key.
C:\>cd \java\src C:\java\src>javac MooseGreetings.java
The class should be compiled, and a new file called MooseGreetings.class should be created in the same directory. If a compiler error happens, make sure you copy/pasted the above code properly. If the file is not found, make sure you're in the right directory, and that your file is properly named MooseGreetings.java. If you can't figure out why it does not compile, ask at the Beginner Java forum.
Let's execute the program. In the same directory, type the following command : "java -classpath . MooseGreetings".
C:\java\src>java -classpath . MooseGreetings mooooooooooo C:\java\src>
Greetings for the Javaranch Moose should be printed : mooooooooooo.
That's it ! You've successfully executed your first Java program. You're now ready to get into the Java world ! If you have any questions, some friendly folks will be happy to help you at the Beginning Java forum.
The -classpath flag (or -cp flag) may be used at the command prompt to tell Java where to look for .class files. Your class files may be in directories, or in JAR files. These can be set in the classpath either by setting the CLASSPATH environment variable, or by using the -classpath flag. Details about setting the CLASSPATH are in the HowToSetTheClasspath FAQ. In our example, "-classpath ." means that Java should scan the current directory (".") for class files.