Halloween BF
Save up to 80 % on our HTML/CSS and JavaScript e-learning courses. Only this week!
Get up to 80 % extra points for free! More info

Lesson 2 - NetBeans IDE and your first console application

Lesson highlights

Are you looking for a quick reference on creating a Java SE project in the NetBeans IDE instead of a thorough-full lesson? Here it is:

  • Download the latest OpenJDK at https://adoptopenjdk.net/
  • Install it and during the installation don't forget to check to create and set the JAVA_HOME system variable:
Creating a new project in NetBeans
  • Select the Java - Java Application template:
Creating a new console application in Java
  • Edit the project name and location if needed and confirm the form
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to the Java language, we talked about the language itself and went over how Java works. In today's lesson, we're going to focus on the NetBeans IDE. We'll show you how to use it and program a simple console application.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we'll write a source code, and then use it to run, test and debug our application.

Installation

Lets start by installing NetBeans of course.

JDK

As first we need to download JDK (Java Development Kit) which is a set of tools needed for Java development. There are more distributions available, we'll use AdoptOpenJDK which is free and available at https://adoptopenjdk.net/. We'll select the latest OpenJDK version, keep the JVM selection on the default HotSpot and click the Latest release which starts the download.

When installing, it's important to check that we want the installer to create and set the JAVA_HOME system variable on the component selection screen. Without this system variable, the NetBeans installer would not know where to look for the JDK and could not work:

NetBeans

Now, let's download the IDE. It's a abbrev. of Integrated Development Environment and, simply speaking, it's an application in which we'll write our source code, run it and debug it. There are many different IDEs available, e.g. Eclipse. We'll use Apache NetBeans which is available for free at https://netbeans.apache.org/…d/index.html. We select the latest available version and click the Download button. On the next page, we select the version for Windows-x64 in the Installers section on finally select the mirror to download from on the last page, usually the first link is the best choice.

Install it (just keep clicking Next) and run it.

Backup and version control

Programmers usually need a tool that will provide version control and backup of his work. We can't rely on the fact that we could just save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about such situations right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, but its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.

Creating a project

Run NetBeans and select File -> New Project in the application menu.

Creating a new project in NetBeans

In the New Project window, select the Java - Java Application template. In the next menu, name this project FirstApplication. Create a folder for your projects in your Dropbox folder, for example, "java". Use the Browse button and select a folder C:\Users\your_name\Dropbox\java\. We will stick with console applications, command line interface, for a while because it needs minimal knowledge of the object-oriented world, and they are ideal for learning the basics of the language. The window should look like this:

Creating a new console application in Java

Confirm the form.

NetBeans Tutorial

The window now looks like this (I have resized it to fit :) ):

NetBeans Java window

We're interested mainly in the middle window in which NetBeans will generate some source code. It may be a surprise to some of you that we are not starting out with an empty window, but then again that is exactly what a template is (easily modifiable preset code)! We'll keep it simple for now, everything will be explained throughout the courses. Some parts are quite advanced, so we'll just accept the fact they're there and go into detail later.

We won't pay any attention to the package and class, just keep in mind that Java uses them to structure programs. The key will be the main() method for us, we'll write our code between the curly brackets below it, i.e. the body. Main() is a reserved word, and Java knows that this method must be executed as first after the application starts (there are more methods, but more on that later). Actually, we'll ignore everything except for the main() method.

The second important element in the window is a green "Play" button in the upper bar, which compiles and runs our program. You can try it, but our program will not do anything, just turn terminate right away. You can also run the program with the F6 keyboard shortcut.

Hello world

As tradition instructs, we'll make the first program most people make when they learn a new language -Hello World. This is a program that displays "Hello world" or some similar text. Again, let me remind you that we'll write commands in the body of the main() method.

To write text we use the following bit of code:

System.out.println("Text");

System is a class. For now, we'll see classes as sets of available commands. In Java, commands are called methods. So System includes methods for console operations. We call the println() method, which displays a text, on the out (as output) object. You can see that we use a dot operator to call the method in the class. Methods can receive input parameters, which are entered in parentheses and separated by commas. In the case of the println() method, the parameter is the text to be printed. In programming, texts are called strings, like a string of characters, and written it in quotation marks. Java sees the quotation marks and interprets it as text so it's not confused with other commands. We should write the commands on separate lines, and after every line, we should put a semicolon. Our main() method will now look like this:

public static void main(String[] args)
{
    System.out.println("Hello ICT.social!");
}

You can run the program by pressing F6 if your system hasn't assigned a different command for the key. You can also run code snippets from our articles by pressing the "Play" button above, you can edit the code as well, just click on it. Try that :) This makes you able to study from anywhere, even without your IDE.

FirstApplication
Hello ICT.social!

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables, type system and parsing in Java, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file at the end of the article. You could always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find a mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Download

Downloaded 185x (15.01 kB)
Application includes source codes in language Java

 

Previous article
Introduction to the Java language
All articles in this section
Java Basic Constructs
Article has been written for you by David Capka
Avatar
Do you like this article?
9 votes
The author is a programmer, who likes web technologies and being the lead/chief article writer at ICT.social. He shares his knowledge with the community and is always looking to improve. He believes that anyone can do what they set their mind to.
Unicorn university The author learned IT at the Unicorn University - a prestigious college providing education on IT and economics.
Activities (12)

 

 

Comments
Display older comments (12)

Avatar
stanley kariuki:5/20/2017 6:24

hallo Dave?should i delet the wors written in fedded ink,mine doest run,shows errors

 
Reply
5/20/2017 6:24
Avatar
stanley kariuki:5/20/2017 6:49

hey,what area did you put the codes

 
Reply
5/20/2017 6:49
Avatar
Arthur Kateeba Ssekikubo:5/21/2017 11:24

Well i have installed the netbeans and jdk 8 and also done with the first lesson. I did notice though that the class first letter has to be in uppercase.

Reply
5/21/2017 11:24
Overcoming fear only makes you stronger
Avatar
Akintunde Richard:6/5/2017 11:00

David thanks so much for this lesson. It's so exciting.

 
Reply
6/5/2017 11:00
Avatar
Shecy
Member
Avatar
Shecy:6/14/2017 6:30

Please state that the commands/class "System" is case sensitive! i spent a lot of time figuring that out! thanks.. nice work

 
Reply
6/14/2017 6:30
Avatar
Georges Maina Brown:6/20/2017 16:37

nice one. thanks guys

 
Reply
6/20/2017 16:37
Avatar
Dorkky Olalekan Paul:7/2/2017 15:39

I really need your help about the first assignment the program fail to run because of "printIn". I don't know how to type the "In" or it it 1 please I don't know how to write it

 
Reply
7/2/2017 15:39
Avatar
David Capka
ICT.social team
Avatar
Replies to Dorkky Olalekan Paul
David Capka:7/2/2017 18:40

It's not In, it's Ln like Line :)

Edited 7/2/2017 18:40
Reply
7/2/2017 18:40
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Alex CHERNOV
Member
Avatar
Alex CHERNOV:26. July 7:52

Please notice somewhere that in netbeans v.11 needed template is in folder "Java with Ant" > "Java Application".

 
Reply
26. July 7:52
Avatar
Replies to Alex CHERNOV
Sudeep Sangamesh Babu :16. August 4:19

@Alex Chernov: Thanks mate, I was trying to figure out what went wrong!

Reply
16. August 4:19
Expand your mind! DON'T STOP!
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

10 messages from 22 displayed. Show all