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.
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
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.
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.
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
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.
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 :
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.
Let's execute the program. In the same directory, type the following command : "java -classpath . MooseGreetings".
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.
Note: If you are using Ubuntu, skip this part and go directly to the Ubuntu installation instructions.
- Go to the official Java SE download page
- 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.
- At the download page, select your target platform, which includes Windows, Linux and others. Save the file in a directory of your choice.
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
- Double-click the file you have downloaded.
- 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.
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
- Set the execution permission on the downloaded file : chmod a+x jdk-6u21-linux-i586.bin
- 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
- Execute the installer : ./jdk-6u21-linux-i586.bin
- 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 ).
- Remove the binary file : rm jdk-6u21-linux-i586.bin
- RPM Package
- Change to root using the 'su' command
- Set the execution permission on the downloaded file : chmod a+x jdk-6u21-linux-i586-rpm.bin
- Execute the installer : ./jdk-6u21-linux-i586-rpm.bin
- Follow the installer instructions.
- 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.
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
- Open the Synaptic Package Manager : System->Administration->Synaptic Package Manager.
- In the Quick Search box, type 'sun' and look for 'sun-java6-jdk'.
- Click the check box and click 'Mark for Installation'.
- Click 'Apply'.
- Follow the installer instructions.
- Once the installation is over, you can close the Synaptic Package Manager.
- Configuring your system to use the Oracle JDK by default
- Open a terminal window.
- Check which version of the JDK you have installed : sudo update-java-alternatives -l
- Select the latest Sun JDK to be the default JDK : sudo update-java-alternatives -s java-6-sun
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
- Start/Settings/Control Panel.
- Double-click System, and select the Advanced tab.
- Click the Environment Variables button.
- 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.
- 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)
- Click OK.
- 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.
- Click OK and close all remaining windows.
- Start/Control Panel.
- Double-click System, and select "Advanced system settings" on the left.
- On the "Advanced" tab, click the Environment Variables button.
- 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.
- 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)
- Click OK.
- 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.
- Click OK and close all remaining windows.
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.
- 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.
- 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 /.
- Save .bashrc.
- Open a new shell. Test it with "java -version" and "javac -version". Use "echo $PATH" to see what your path is set to.
- It is possible to add other paths similarly.
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.javaThe 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.
Comments
Post a Comment