0% found this document useful (0 votes)
10 views36 pages

W2-Presentation-Getting To Know The Programming Environment

This document provides a comprehensive guide for beginners on how to set up a Java programming environment, including writing, compiling, and running a simple 'Hello World!' application using both a text editor and the NetBeans IDE. It explains the steps to create a Java program, resolve common errors, and differentiate between syntax, runtime, and logical errors. Additionally, it outlines the features of NetBeans as an Integrated Development Environment for Java programming.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views36 pages

W2-Presentation-Getting To Know The Programming Environment

This document provides a comprehensive guide for beginners on how to set up a Java programming environment, including writing, compiling, and running a simple 'Hello World!' application using both a text editor and the NetBeans IDE. It explains the steps to create a Java program, resolve common errors, and differentiate between syntax, runtime, and logical errors. Additionally, it outlines the features of NetBeans as an Integrated Development Environment for Java programming.

Uploaded by

igcasan.jc07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Getting to know the programming

environment
At the end of this lesson you should be
able to:

• Write simple Java code in Text Editor and Netbeans.

• Compile and run Java code using Command Line and Netbeans.

• Differentiate the three types of error, the syntax errors, runtime errors and
logical errors.
A. The “Hello World!” application, your
first Java Program
The “Hello World!” application, your first Java Program

public class HelloWorld{


public static void main(String args[]){
System.out.println("Hello World!");
}
}
The following are the steps to create the
program:
Step1 : Start the text editor.
Step 2: Write the source code in Notepad text editor.
3. Save the source file.

To save the file in notepad, on the menu bar, The Save dialog box will show up, navigate to drive
click file then select "save as". 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.
Step 4. Compiling the source code using the windows command
line.

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.
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.
To compile the Java source file we need to enter “JavaC [filename]” in the command line, in
our case we should enter “JavaC HelloWorld.java”.
As we discuss in the phases of a Java program, the compilation process generates bytecodes,
in this case the HelloWorld.class will be created and this contains the actual Java bytecode.
To run the Java program, we need to enter “Java [filename]” or in our case “Java
HelloWorld”. You have just run your first program and displayed the message, “Hello
World!”
Resolving an error: "javac is not recognized as an internal or external
command"
If there is a problem occuring during the compilation like the figure below,
we need to set the JavaC path in the command prompt because Java compiler or
JavaC does not recognize in command prompt.
Two ways to set Java path, one is temporary and other one is permanent.
1.Setting the temporary path of JavaC
• Launch command prompt
• Enter the path command, for example, “path C:\Program Files\Java\jdk1.7.0_03\bin”
2.Setting permanent path of JDK.
a. On your desktop, right click the computer b. Click the advance system settings
icon, then click properties.
c. Select the Advance tab and click the
environment variables d. Click the new button of user variable.
e. On the variable name field enter the “path” f. Copy the path of the JDK bin folder.
word
g. Paste the copied path in the variable value field. Then click all the OK buttons from all the windows
you have opened.
B. Errors
There are three types of error that you might encounter in Java
programming:
Syntax Errors
• Misspelled keyword, variable and method names or incorrect used of
capitalization.
• Missing semi-colon at the end of the statement or incorrect used of symbols.

• Brackets such as curly braces “{}”, parentheses “()“ and square brackets “[]” is
not properly matches.
Runtime Errors
-occurs after compilation (when there is no syntax error in the
program) and running your program.

Runtime is when the program is running therefore you can only encounter
runtime error during execution of the program or when you are using your
program.
The figure below shows that the program was
successfully compiled and didn’t produce syntax
error, however when we tried to run the program,
we encountered runtime error

The figure above is a sample program that has


no syntax error, but will produce runtime error
and it has a statement that performs a division
by zero
Logical Errors

The common examples of these are:

• Incorrect used of mathematical operation like using of + symbol for


subtraction.
• Displaying the incorrect message.
• Using data from incorrect source.
The figure below is an example that demonstrates a logical error.
Using NetBeans

• Is an Integrated Development Environment or IDE, it is a programming


environment that provides comprehensive facilities to programmers for
Developing Java programs. NetBeans contains a source code editor, GUI
Builder, Compiler, interpreter and a debugger.
• It is the easiest way of writing and running the Java program.
Step 1: Start NetBeans

To open the NetBeans program, double The figure below shows the graphical user interface
click the NetBeans shortcut icon in your (GUI) of the NetBeans IDE.
desktop.
Step 2. Creating a project.
After clicking the New Project, a New Project
To create a project, in the IDE
dialog bar will show up. On the category list, select
menu bar choose File then click
Java and on the Project list, select the Java
New Project.
Application, then click the Next button.
In the New Application Dialog, do the following (as shown in the figure below):

• In the Project Name field change the value to


JavaPrograms.
• Leave the default value of the Project Location, by
default our project is located in
C:\Users\<user>\Documents\NetBeansProjects
directory and all the files will be saved in
C:\Users\<user>\Documents\NetBeansProjects\JavaPr
ograms.
• Leave the Use Dedicated Folder for Sharing Libraries
unchecked.
• On the Create Main Class field, enter “HelloWorld” and
then click the Finish button.
The Java Application project is now created and
opened in the IDE. The IDE will have the
following components:

Projects window displays all projects


loaded in the IDE and it is presented in a tree
view. This window shows the components of the
project such as source files, libraries, etc.

Source Code Editor Window where you


can write Java source code.

Navigator window where you can


navigate elements within the selected class.
Step 3. Writing your program in
NetBeans IDE...
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.
Let us now create a program that
displays “HelloWorld” message. Replace
the line of code:
Save the code by selecting File on
the menu bar and then click save or we
can simply press CTRL + S.
Step 4. Compiling and Running your program in NetBeans IDE.
There are three ways to run your program in NetBeans IDE:
• Select the Run on the menu bar then click Run Project.

• Click the Run Icon on the Tool bar


• Press F6

You might also like