Getting To Know The Programming Environment: Module of Instruction
Getting To Know The Programming Environment: Module of Instruction
Getting To Know The Programming Environment: Module of Instruction
In this section we are going to write, compile and run a Java program
in Text Editor and Command Line. We are going to create a simple
program that will display the words “Hello World!”. Below is the
source code of our first program:
public class HelloWorld{
public static void main(String args[]){
System.out.println("Hello World!");
}
}
Before we can run any Java program, we will need the following:
Let’s start creating the “Hello World” program. The following are the
steps to create the program:
Computer Programming 2 1
Week 2 Getting to know the programming environment
2
MODULE OF INSTRUCTION
To save the file in notepad, on the menu bar, click file then select
"save as".
Computer Programming 2 3
Week 2 Getting to know the programming environment
The Save dialog box will show up, navigate to drive C and then select
the folder that you created a while ago.
On the file name input box, enter "HelloWorld.java" and change the
"Save as type" to "All Files" and then click save.
4
MODULE OF INSTRUCTION
The first thing that we need to do to compile the source code is launch
the command prompt, click the start menu and on search bar type
command or cmd.
When the command prompt open up, by default it will take you to the
current user directory. In the example below the default directory is in
Administrator.
Computer Programming 2 5
Week 2 Getting to know the programming environment
We need to navigate to the folder where the Java source code was
saved or we need to go to “C:\JAVAPROGRAMS”. On the command
line type “cd C:\JAVAPROGRAMS” then press enter. You are now
inside the JAVAPROGRAMS directory. The “cd” command stands
for change directory; it is used to change the current directory.
Let us check whether we are in the right directory and see if the Java
source file was saved in this directory. On the command line type “dir”
then press enter, your Java source file should be displayed. “dir”
command is used to display files and subdirectories inside a directory.
6
MODULE OF INSTRUCTION
Computer Programming 2 7
Week 2 Getting to know the programming environment
8
MODULE OF INSTRUCTION
There are two ways to set Java path, one is temporary and other one is
permanent.
Computer Programming 2 9
Week 2 Getting to know the programming environment
10
MODULE OF INSTRUCTION
Computer Programming 2 11
Week 2 Getting to know the programming environment
12
MODULE OF INSTRUCTION
Computer Programming 2 13
Week 2 Getting to know the programming environment
B. Errors
14
MODULE OF INSTRUCTION
Syntax errors
Runtime errors
Logical errors
Syntax Errors
The errors occur when the syntax of the language is
violated, specifically when a word or symbol was not correctly
placed in an instruction or statement of the program.
Computer Programming 2 15
Week 2 Getting to know the programming environment
Runtime Errors
16
MODULE OF INSTRUCTION
Logical Errors
Computer Programming 2 17
Week 2 Getting to know the programming environment
18
MODULE OF INSTRUCTION
Using NetBeans
In our previous discussion, we tackled the hard way of
running Java program. Let’s now try the easiest way of
writing and running the Java program.
Computer Programming 2 19
Week 2 Getting to know the programming environment
20
MODULE OF INSTRUCTION
Computer Programming 2 21
Week 2 Getting to know the programming environment
22
MODULE OF INSTRUCTION
Since you have left the Create Main Class checked in the
New Application dialog the NetBeans IDE have generated
HelloWorld.java file that contains code that is shown in the
source code editor window.
System.out.println(“Hello World!”);
Computer Programming 2 23
Week 2 Getting to know the programming environment
Save the code by selecting File on the menu bar and then click
save or we can simply press CTRL + S.
24
MODULE OF INSTRUCTION
Press F6
Computer Programming 2 25
Week 2 Getting to know the programming environment
LESSON SUMMARY:
1. You can write java program using notepad and compile it using
windows command line.
3. There are three types of error in java these are Syntax errors, run
time errors and logical errors.
26