You are on page 1of 11

Introduction to Java (Part 1)

Objective
At the end of this lab, the student is able to: Install a Java compiler
Compile and Debug Java programs
Run Java programs

Installing Java Compiler


A Java program can be run on any machine that is installed with Java Virtual Machine. The following are the
steps to install a java compiler.
1. Download Java Development Kit (JDK) from
http://www.oracle.com/technetwork/java/javase/downloads/index.html. Save it in a folder named Java in
drive C.
2. Run the downloaded installer (e.g., "jdk-8u{xx}-windows-x64.exe"), which installs both the JDK
and JRE.

3.
4.

Double click on the JDK folder. You should be able to see a bin folder. Copy the location of the bin
folder and paste it on the notepad. Example : C:\Program Files\Java\jdk1.8.0_25\bin. You need to set
the environment variables to enable you to compile anywhere in your computer.
Go to Control Panel and double click on System icon. A System Properties window will appear on your
screen as shown in Figure 1.

Figure 1 : System Properties Window

5.

Go to Advanced tab and click on the Environment Variables button as shown in Figure 2. Environment
Variable window will be shown on your screen as depicted in Figure 3.

Click here

Figure 2 : Advanced Tab

Figure 3 : Environment Variables Window


6.

Click on PATH variable in User Variable section and click Edit. Edit User Variable will appear on your
screen as show in Figure 4.

Figure 4 : Edit User Variable Window

7.

Add the full path of java bin location at the end of variable value as shown in Figure 5. Example : ;
C:\Program Files\Java\jdk1.6.0_05\bin;. Then, Click OK.

Figure 5 : Setting Java Environment Variable


8.
9.

Then click OK on the Environment Variable window.


Go to command prompt, and type javac as shown in Figure 6.

Figure 6 : Execute javac on Command Prompt

10. If you get the following message as shown in Figure 7, you have successfully installed a Java compiler.

Figure 7 : javac Messages

Writing a Java Program


A Java filename must be the same with the class name declared in the source code. For example if the class
name is declared as public class Greeting, then the source code is saved as
Greeting.java, as shown in Figure 8.

Figure 8 : Saving a Java class


A Java program is saved with .java extension.
A good java class name starts with uppercase letter and it follows camel humpback notation. If the class name
is more than one word, the subsequent name will starts with uppercase letter. A Java class name cannot have a
space in between. Avoid using underscore _ for the class name.
Example of good class names:a. class Student
b. class PostGraduateStudent

Compiling a Java Program - javac


A Java class is compiled using javac at command prompt. javac compiles a Java class and produce a byte
code file with .class extension. The compiler checks the program compliance to Java syntax and semantic.
Figure 9 shows compiling a java program. To compile a Java program type javac followed by the complete
class name with .java.

Figure 9 : Compiling a Java Program


If the program is error free, a .class file will be produce, Greeting.class and the prompt is ready for the next
command, otherwise the compiler will list all the errors found in the program.

Debugging a Java Program


Errors might occur at compile time or runtime. Errors at compile time easier to debug compare to the runtime
error. Figure 10 shows a sample of a Java program error.
Greeting.java:11: class greeting is public, should be declared in a file named greeting.java
public class greeting {

^
1 error

Figure 10 : A Java Program Error


The programmer has to open the source code to fix the error. The source code needs to be re-compile after the
error is fixed before the program is executed. Figure 11 describe how to read Java error message.

File name where error


occur
Line number

Description of error

Greeting.java:11: class greeting is public, should be declared in a file named greeting.java


public class greeting {
^
1 error

The exact place where


the error should be fix

Total error
Figure 11 : Description of Error

Running a Java Program - java


A Java program can be run if the respective .class file is successfully created. The command to run a java
program is java. Type java followed by the class name without any extension. Figure 12 shows how to run a
java program.

Figure 12 : Running a Java Program

Exercises
Exercise 1
1. Compile Greeting.java from Lab1/Exe1.
2. You will get the following compile error.
Greeting.java:11: class greeting is public, should be declared in a file named greeting.java
public class greeting {
^
1 error
3. Fix the error and re-compile Greeting.java. The program shall produce Greeting.class.
4. Run class Greeting. The program shall produce Welcome to Java on console.
Exercise 2
1. Compile Greeting.java from Lab1/Exe2.
2. The program will produce several errors.
3. Fix the errors and re-compile Greeting.java. The program shall produce Greeting.class.
4. Run class Greeting. The program shall produce Java is Cool on console.
Exercise 3
1. Compile Greeting.java from Lab1/Exe3.
2. The program will produce several errors.
3. Fix the errors and re-compile Greeting.java. The program shall produce Greeting.class.
4. Run class Greeting. The program shall produce I LOVE JAVA PROGRAMMING on console.
Exercise 4
1. Compile Greeting.java from Lab1/Exe4.
2. The program will produce several errors.
3. Run class Greeting. The program shall produce runtime error message.
4. Fix the error and re-compile Greeting.java. The program shall produce Greeting.class.
5. Run class Greeting. The program shall produce Eat. Drink. Sleep. Drink. Java on console.
Exercise 5
1. Compile Greeting.java from Lab1/Exe5.
2. The program will produce an error.
3. Fix the error and re-compile Greeting.java. The program shall produce Greeting.class.
4. Run class Greeting. The program shall produce Java Rules on console.
Exercise 6
1. Compile MyClass.java from Lab1/Exe6.
2. The program will produce several errors.
3. Fix the errors and re-compile MyClass.java. The program shall produce MyClass.class.
4. Run class Greeting. The program shall produce Congratulation!!! Youve made it!!! on console.
Exercise 7
1. Run class DemoClass from Lab1/Exe7.
2. The program shall produce Your journey mastering Java begins now on console.
Exercise 8
1. Compile DemoClass.java from Lab1/Exe8.
2. The program will produce several errors.
3. Fix the errors and re-compile DemoClass.java. The program shall produce DemoClass.class.

4.

Run class Greeting. The program shall produce Its a rough ride ahead. Brace yourself!!! on console.

Exercise 9
1. Compile DemoClass.java from Lab1/Exe9.
2. The program will produce several errors.
3. Fix the errors and re-compile DemoClass.java. The program shall produce DemoClass.class.
4. Run class Greeting. The program shall produce I come, I see, I conquer on console.
Exercise 10
1. Compile DemoClass.java from Lab1/Exe10.
2. The program will produce several errors.
3. Fix the errors and re-compile DemoClass.java. The program shall produce DemoClass.class.
4. Run class Greeting. The program shall produce
+-------+
| SPAIN |
+-------+
on console.
Exercise 11
1. Compile WorldClass.java from Lab1/Exe11. The program shall produce DemoClass.class.
2. Run class WorldClass. The program produce a runtime error.
3. Fix the errors and re-compile WorldClass.java.
4. Run class WorldClass. The program shall produce ITS A FLAT WORLD on console.

You might also like