Introduction to Compiling and Running An Application
Installing the Java Standard Edition Development Kit (JDK)
Before you can run Java examples of DeveloperConcepts , you must install the Java Standard Edition Development Kit (JDK) 6 or a Java development tool that supports Java SE 6.
You can download the JDK 6 and its documentation from java.sun.com/javase/6/download.jsp. Click the » DOWNLOAD button for JDK 6. You must accept the license agreement before downloading. Once you accept the license agreement, click the link for your platform’s installer. Save the installer on your hard disk and keep track of where you save it. Before installing, carefully read the JDK installation instructions for your platform, which are located at java.sun.com/javase/6/webnotes/install/index.html.
After downloading the JDK installer, double click the installer program to begin installing the JDK. We recommend that you accept all the default installation options. If you change the default installation directory, be sure to write down the exact name and location of the directory you choose, as you will need this information later in the installation process. On Windows, the JDK is placed in the following directory by default: C:Program FilesJavajdk1.6.0
Setting the PATH Environment Variable
The PATH environment variable on your computer designates which directories the computer searches when looking for applications, such as the applications that enable you to compile and run your Java applications (called javac.exe and java.exe, respectively). You will now learn how to set the PATH environment variable on your computer to indicate where the JDK’s tools are installed.
- Opening the System Properties dialog. Start > Control Panel > System to display the System Properties dialog. This particular dialog is from a computer running Microsoft Windows XP.
- Opening the Environment Variables dialog. Select the Advanced tab at the top of the System Properties dialog. Click the Environment Variables button to display the Environment Variables dialog
- Editing the PATH variable. Scroll down inside the System variables box to select the PATH variable. Click the Edit button. This will cause the Edit System Variable dialog to appear.
- Changing the PATH. Place the cursor inside the Variable Value field. Use the leftarrow key to move the cursor to the beginning of the list. At the beginning of the list, type the name of the directory in which you placed the JDK followed by bin;. Add C:Program FilesJavajdk1.6.0bin; to the PATH variable, if you chose the default installation directory. Do not place any spaces before or after what you type. Spaces are not allowed before or after each value in an environment variable. Click the OK button to apply your changes to the PATH variable.
After setting PATH Environment variable, open command prompt, write java and press enter. If the PATH variable is set properly then you should see different java options. If you do not set the PATH variable correctly, when you use the JDK’s tools, you will receive a message like:
'java' is not recognized as an internal or external command, operable program or batch file.
In this case, go back to the beginning of this section and recheck your steps. If you’ve downloaded a newer version of the JDK, you may need to change the name of the JDK’s installation directory in the PATH variable.
Setting the CLASSPATH Environment Variable
CLASSPATH Environment variable is used to specify path of class files of our source code and libraries which we use in our programs. Create CLASSPATH variable by following PATH Environment variable steps.
To write a Java program, we need Java Development Kit (JDK) and a text or Java editor. We will simple text editor like Notepad.
A simple editor helps a lot in understanding of basic concepts of a programming language. In start of programming practice, this is a good technique to write code in a simple editor rather than powerful IDE. A powerful IDE does lot of things automatically for you and you may miss some basic details. I wrote my first program in notepad and compiled and executed it on command prompt.
In our Java topics, we will write consol applications. Console applications perform all of their input and output at the command line, so they are ideal for quickly trying out language features and writing command line utilities.
HelloWorld Example
A Java program consists of statements. All tasks that you want to accomplish in a Java program can be broken down into a series of statements. In a programming language, a statement is a simple command that causes something to happen. Statements represent a single action taken in a Java program.
Open Notepad and write your first Java consol application. We will write a basic HelloWorld application which prints “Hello World” message on consol.
Copy and paste following example in Notepad and the save source code as .java file. Now open DOS prompt and point to directory where you have saved the .java file.
Use following command to compile the example:
d:Java Examples>javac HelloWorld.java
If you do not find any error after compilation of the example, it means you have successfully compiled the application.
After successful compilation, use following command to run the example:
d:Java Examples>java HelloWorld
Example
1. /**
2. * @(#)HelloWorld.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/10/21
7. */
8. public class HelloWorld {
9.
10. /**
11. * Creates a new instance of HelloWorld.
12. */
13. public HelloWorld() {
14. }
15.
16. /**
17. * @param args the command line arguments
18. */
19. public static void main(String[] args) {
20. // TODO code application logic here
21. }
22. }
Java programs are composed by classes. “A class is an object-oriented concept which is used to describe the properties and behavior of a real world entity.” by Roger Pressman.
In above example, line numbers are not part of the program. They are used to explain Java statements by their respective line number. On line 8, HelloWorld is class name. Keep in mind that file name and the program class name must be same otherwise it is a program error. In the above class, HelloWorld() is a constructor of the HelloWorld class. Class name and constructor name are identical. The constructor is used to initialize data members or properties of the class. In the class, main() is a special class method which is used to run Java application.
The text enclosed by /* */ text are the program comments. The comments are used to improve readability of your program for humans. The Java compiler ignores comments entirely when preparing a runnable version of a Java source file.
Compile and execute the Java program. You will not see any message from application because we didn’t write anything in main() method.
Add following Java statement in main() method and compile and run the program again:
System.out.println("Hello World");
Output
Hello World
This time, you will see “Hello World” message. In a Java program, the statement System.out.println(“Hello World”); is used to print message on consol. The “;” is terminator which is used to terminate a Java statement.
Keep in mind that Java programming language is case sensitive so class, variable, and method names should be consistent in a Java program.
