How to create your first java program

Downloading the Java SDK  
Note: If you are using Ubuntu, skip this part and go directly to the Ubuntu installation instructions.

  1. Go to the official Java SE download page
  2. Download the latest Java Platform, Standard Edition. You do not need to download the JRE. Download the JDK, which includes the JRE. If you're interested to know the difference between the JDK and the JRE, check our JavaBeginnersFaq.
  3. At the download page, select your target platform, which includes Windows, Linux and others. Save the file in a directory of your choice.
Installing the Java SDK  
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.
Windows
  1. Double-click the file you have downloaded.
  2. Follow the installer instructions. You don't need to change the default options. You may just change the directory where you want to install the JDK. After the installation is over, make sure that the directory you have chosen has been created.
Linux
You have to choose whether you want to use a self extracting binary file or use an RPM package. If your system is RPM-based, you may wish to use the RPM package isntead of the binary file.



Self extracting binary file
    1. Set the execution permission on the downloaded file : chmod a+x jdk-6u21-linux-i586.bin
    2. Move the file to the directory where you wish to install java (e.g. /usr/java). Make sure you have writing permission on that directory
    3. Execute the installer : ./jdk-6u21-linux-i586.bin
    4. Follow the installer instructions. After the installation is over, the JDK directory should be created under the current directory (e.g /usr/java/jdk1.6.0_21 ).
    5. Remove the binary file : rm jdk-6u21-linux-i586.bin

RPM Package
    1. Change to root using the 'su' command
    2. Set the execution permission on the downloaded file : chmod a+x jdk-6u21-linux-i586-rpm.bin
    3. Execute the installer : ./jdk-6u21-linux-i586-rpm.bin
    4. Follow the installer instructions.
    5. After the installation is over, the installer should have created to symbolic links : /usr/java/latest and /usr/java/default. The /usr/java/latest link will tell you where your JDK is installed. For more information about these links, check the official install instructions.
Ubuntu  
Java should already be installed in your system, but the JDK used should be Open JDK, not Sun JDK. If you want to use Sun JDK, you have to first install it, and then configure your system to use it.

Installing the JDK via the Synaptic Package Manager
    1. Open the Synaptic Package Manager : System->Administration->Synaptic Package Manager.
    2. In the Quick Search box, type 'sun' and look for 'sun-java6-jdk'.
    3. Click the check box and click 'Mark for Installation'.
    4. Click 'Apply'.
    5. Follow the installer instructions.
    6. Once the installation is over, you can close the Synaptic Package Manager.

Configuring your system to use the Oracle JDK by default
    1. Open a terminal window.
    2. Check which version of the JDK you have installed : sudo update-java-alternatives -l
    3. Select the latest Sun JDK to be the default JDK : sudo update-java-alternatives -s java-6-sun
Mac OS
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 !
Setting the JAVA_HOME and PATH environment variables  
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 XP
  1. Start/Settings/Control Panel.
  2. Double-click System, and select the Advanced tab.
  3. Click the Environment Variables button.
  4. You can set environment variables for either your user only, or for all users (System variables). But don't set a user JAVA_HOME and a system PATH (see this discussion). Add a new variable in either of them by clicking the New button.
  5. Set the variable name as JAVA_HOME, and its value to the directory where you installed the JDK (e.g. C:\java\jdk1.6.0_21)
  6. Click OK.
  7. Look for a variable called PATH (the name might slightly vary, for example 'Path'). Select it and click Edit. At the beginning of its value, add the following : "%JAVA_HOME%\bin;" (without the double quotations). Don't forget the semi-colon at the end, to separate this directory to the other directories already in the path.
  8. Click OK and close all remaining windows.
Windows Vista / Windows 7
  1. Start/Control Panel.
  2. Double-click System, and select "Advanced system settings" on the left.
  3. On the "Advanced" tab, click the Environment Variables button.
  4. You can set environment variables for either your user only, or for all users (System variables). But don't set a user JAVA_HOME and a system PATH (see this discussion). Add a new variable in either of them by clicking the New button.
  5. Set the variable name as JAVA_HOME, and its value to the directory where you installed the JDK (e.g. C:\java\jdk1.6.0_21)
  6. Click OK.
  7. Look for a variable called PATH (the name might slightly vary, for example 'Path'). Select it and click Edit. At the beginning of its value, add the following : "%JAVA_HOME%\bin;" (without the double quotations). Don't forget the semi-colon at the end, to separate this directory to the other directories already in the path.
  8. Click OK and close all remaining windows.
Linux
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.
  1. Open the file called ".bashrc" in your home directory, with a text editor. From the shell, one can enter "gedit .bashrc" or "gedit ~/.bashrc", to use gedit. Don't forget the dot which means it is a hidden file.
  2. Add a line with the Java path at its beginning. It will read like this "export PATH=/usr/java/jdk1.6.0_22/bin:$PATH". Note there is only one space, after export. The Java path begins with / and ends with bin, and paths are separated by : colons. If you have installed Java in your "home" folder, you may be able to use a path beginning with ~ instead of /.
  3. Save .bashrc.
  4. Open a new shell. Test it with "java -version" and "javac -version". Use "echo $PATH" to see what your path is set to.
  5. It is possible to add other paths similarly.
Mac OS
Instructions for Mac OS coming soon...
What is the purpose of these variables ?  
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".
Checking that your JDK is configured properly  
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.
Creating your first Java program  
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 :

public class MooseGreetings {
    public static void main(String[] args) {
        System.out.println("mooooooooooo");
    }
}

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 !
Compiling and executing the program  
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.
What's that fancy -classpath flag used when executing a Java program ?  
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.

Share this

Related Posts

Previous
Next Post »