HaQT Fundamental Programming
Getting Started with Java
1 Elements of a Java Application
I understand that you are eager to type some code in your editor and run it to see your first
Java application in action! Do not worry, your expectation will be fulfilled by the end of this
tutorial. But before we move on, I would like to do through several elements that you need
to know to fully understand what you are doing.
Even if you are familiar with some other programming language, know about compilation,
know what an executable file is you may be interested in the following because Java works
in a way that differs from C or C++.
2 Compiling and Executing Java Code
There are several steps that you need to follow to create a Java application. This tutorial
shows you how to create a very simple Java application. If you need to create an enterprise
application, the creation process is more complex but at its core you will find these simple
steps.
The first of these steps is to write some Java code in a text editor.
Then this code has to be transformed to another format, which can be executed by your
computer. This transformation is conducted by a special piece of software called a compiler.
Some languages do not have a compiler; Java does. Every compiler is specific to a language.
The file produced by a compiler is often called a binary file or an executable file. Whereas
you can read a source code and understand it, binary or executable files are not meant to
be read by a human person. Only your computer can make sense of it.
This code contains special binary codes called byte code. This is a technical term that you
may come across. The precise description of what is this byte code is beyond the scope of
this tutorial.
Compiling some code may fail; your code has to be correct for the compiler to produce an
1
HaQT Fundamental Programming
executable version of it. Do not worry, this page gives you the code you are going to compile.
All you need to do is copy it and paste it in your text editor.
Once the compiler produced the binary file that you need, you can execute this binary file,
that will your program.
These two steps: compilation and execution require two specific pieces of software that
are part of the Java Development Kit, also known as the JDK. You will see how to
download the JDK for free and how to install it later in this tutorial.
Note that starting with Java SE 11 you can also merge these two steps into one, by executing
a .java file directly. You can use these feature only if you are executing a program that is
written in a single file. This way of executing your java application does not work if your
java code spans more than one file.
3 Creating a First Java Class
The first step you need to know is that the Java code you are writing is saved in plain text
files. In this tutorial, your application will be written in a single text file. Larger applications
may require thousands of such files.
Java is an object-oriented language. If this technical term does not mean anything to you,
do not worry, all you need to remember at this point is that all the code you write must be
held in a Java class.
A Java class is created by a special declaration in a text file. Just copy the following code
and paste it in your text editor. Congratulation! You have created your first Java class!
1 public c l a s s MyFirstClass {
}
The name of this Java class is MyFirstClass. You need to save this text in a file named
[Link]. A Java class must be saved in a file that has the same name as your class
with the extension .java. This is mandatory and is in fact very convenient because you do
not need to open a file to know what class is written in it.
2
HaQT Fundamental Programming
You can give this class any name as long as it does not start with a number. There is
a convention though: the name of a Java class starts with a capital letter. This is not
mandatory but all Java developers follow this convention. When you become a seasoned
Java developer, seeing a class that does not follow this convention will look weird to you.
If you are following this example to the letter, you should save the MyFirstClass class in a
text file called [Link].
Just a word of warning: you should be using a plain text editor to create and save this file.
Using a word processor will not work.
4 Preparing the Compilation of your First Class
Compiling is the second step you need to follow after the creation of your first class. It
consists of transforming the Java code you wrote in your [Link] file into another
format that can be executed. The result of this transformation will be stored in another file
created by the compiler. The name of this file will be [Link].
So far the only tool you have been using is a plain text editor. Compiling this class requires
a compiler; something you may not have on your computer. Fortunately, you can download
this compiler and use it for free. Let me guide you through this process.
As of now, downloading ”Java” means downloading the Java Development Kit, also known
as the JDK. The JDK contains many tools and among them are the ones you will be using
to compile and run a Java application. It is officially distributed by the OpenJDK project
and by Oracle.
You may have heard about some other elements, also called ”Java”.
The JRE stands for Java Runtime Environment. It is a subset of the JDK that is not
distributed by the OpenJDK or Oracle anymore. It only contained the tools needed to run
a Java application. You cannot compile your code with the tools provided in the JRE.
You may also have heard about J2EE, Java EE or Jakarta EE. All these acronyms refer
to the Java Enterprise Edition. It is a set of tools and libraries to create enterprise-class
applications. Java EE is different from the JDK and is outside the scope of this tutorial.
You do not need Java EE to compile and run the simple application we are creating in this
tutorial.
3
HaQT Fundamental Programming
5 Setting up a Java Development Kit
You can download the JDK from different places. There is a one-stop page that always refers
to the latest version of the JDK: [Link] Selecting the latest ”Ready for
use” JDK version takes you to a page where you can download the version of the JDK you
need.
From this page you can download five versions:
Linux/AArch64
Linux/x64
macOS/x64
macOS/AArch64
Windows/x64
This page provides production-ready open-source builds of the Java Development Kit, an
implementation of the Java SE Platform under the GNU General Public License, version 2,
with the Classpath Exception.
5.1 Setting up a JDK for Windows/x64
Let us download the Windows version. What you get is a ZIP file of about 200MB that you
can open with any ZIP utility software. This ZIP file contains the JDK. You can unzip the
content of this file anywhere on your computer.
Once this is done you need to create an environment variable called JAVA HOME that points
to the directory where you unzipped the JDK. First you need to open a DOS prompt. If you
unzipped a JDK 20 ZIP file in the D:\jdk\ directory then the command you need to type
in this DOS prompt is the following:
Command window
> s e t JAVA HOME=D: \ j d k \ jdk =20
4
HaQT Fundamental Programming
Please note that in this example and all the others the leading > is there to show you that
you need to type this command or paste it in a prompt. You should not type this character
or paste it as it is not part of the set command.
You can check that the JAVA HOME variable has been properly set by typing the following
code:
Command window
1 > echo %JAVA HOME%
This command should print the following:
Command window
1 D: \ j d k \ jdk =20
You then need to update your PATH environment variable to add the bin directory of your
JDK directory to it. This can be done with the following command:
Command window
1 > s e t PATH=%JAVA HOME%\b i n ;%PATH%
You need to be very cautious while setting up these two variables, because a single mistake
like an added white space of a missing semicolon will result in failure.
Do not close this command prompt. If you close it and open it again then you will need to
create these two variables again.
5.2 Setting up a JDK for Linux/x64
Let us download the Linux version. What you get is an archive file with a .[Link] extension
that you need to expand.
To expand it, you need to copy it or move it to the right directory. You can then type the
following command:
5
HaQT Fundamental Programming
Command window
1 $ t a r x z f * . t a r . gz
Please note that in this example and all the others, the leading $ is there to show you that
you need to type this command or paste it in a prompt. You should not type this character
or paste it, as it is not part of the tar command.
This command expands all the files with the extension .[Link] that you have in the current
directory. You can use the exact name of this file if you just need to expand it.
Executing this command may take several seconds or more, depending on your system. It
creates a new directory in the current directory with the content of the JDK in it.
Once this is done you need to create an environment variable called JAVA HOME that points
to the directory where you expanded the JDK. If you expanded a JDK 20 archive file in the
/home/javauser/jdk directory then the command you need to type in this shell prompt is
the following:
Command window
1 $ e x p o r t JAVA HOME=/home/ j a v a u s e r / j d k / jdk =20
The exact directory depends on the distribution file you have expanded.
You can check that the JAVA HOME variable has been properly set by typing the following
code:
Command window
1 $ echo $JAVA HOME
This command should print the following:
Command window
1 /home/ j a v a u s e r / j d k / jdk =20
6
HaQT Fundamental Programming
Then you need to update your PATH variable to add the bin directory of your JDK directory
to it. This can be done with the following command:
Command window
1 $ e x p o r t PATH=$JAVA HOME/ b i n : $PATH
You need to be very cautious while setting up these two variables because a single mistake
like an added white space of a missing semicolon will result in failure.
Do not close this shell prompt. If you close it and open it again then you will need to create
these two variables again.
You can check if everything is ok by typing the following command:
Command window
1 $ which j a v a
Your shell should print the complete path to the java executable file in the bin directory of
the distribution you just expanded. In this example it will print:
Command window
1 /home/ j a v a u s e r / j d k / jdk =20/ b i n / j a v a
5.3 Setting up a JDK for macOS
Let us download the macOS version. What you get is an archive file with a .[Link]—
extension that you need to expand.
To expand it, you need to copy it or move it to the right directory. You can then type the
following command:
Command window
1 $ t a r x z f * . t a r . gz
7
HaQT Fundamental Programming
Please note that in this example, and all the others, the leading $ is there to show you that
you need to type this command or paste it in a prompt. You should not type this character
or paste it as it is not part of the tar command.
This command expands all the files with the extension .[Link] that you have in the current
directory. You can use the exact name of this file if you just need to expand it.
Executing this command may take several seconds or more, depending on your system. It
creates a new directory in the current directory with the content of the JDK in it. This
directory has the extension .jdk.
Once this is done you need to create an environment variable called JAVA HOME that points
to the directory where you expanded the JDK. If you expanded a JDK 20 archive file in the
/Users/javauser/jdk directory then the command you need to type in this shell prompt
is the following:
Command window
1 $ e x p o r t JAVA HOME=/U s e r s / j a v a u s e r / j d k / jdk = 20. j d k / Contents /Home
The exact directory depends on the distribution file you have expanded.
You can check that the JAVA HOME variable has been properly set by typing the following
code:
Command window
1 $ echo $JAVA HOME
This command should print the following:
Command window
1 / U s e r s / j a v a u s e r / j d k / jdk = 20. j d k / Contents /Home
You then need to update your PATH variable to add the bin directory of your JDK directory
to it. This can be done with the following command:
8
HaQT Fundamental Programming
Command window
1 $ e x p o r t PATH=$JAVA HOME/ b i n : $PATH
You need to be very cautious while setting up these two variables because a single mistake
like an added white space of a missing semicolon will result in failure.
Do not close this shell prompt. If you close it and open it again then you will need to create
these two variables again.
You can check if everything is ok by typing the following command:
Command window
1 $ which j a v a
Your shell should print the complete path to the java executable file in the bin directory of
the distribution you just expanded. In this example it will print:
Command window
1 / U s e r s / j a v a u s e r / j d k / jdk = 20. j d k / Contents /Home/ b i n / j a v a
6 Compiling your First Class
Once you have properly set up your JDK; the JAVA HOME variable and the PATH variable,
you are ready to compile your first class.
All the commands you will be typing now should be typed in the same prompt as the one
you used to set up these two variables.
6.1 Compiling and Running Your First Java Program
Whether you followed the Windows, the Linux or the macOS path, the remaining is the
same.
9
HaQT Fundamental Programming
1. Change to the directory where you saved your first class [Link]. You can
check that you are in the right directory by typing dir. It will show you the files you
have in this directory. You should see your [Link] file.
2. Check that your compiler is accessible from this directory by typing the following. This
command is the same whether you are on Windows or Linux.
Command window
1 > j a v a =v e r s i o n
It should tell you which version of the javac you are currently using. If it gives you an error
message then you need to check your JAVA HOME and PATH variables as there is most
probably something wrong with them.
3. Now you are all set to compile your first code. You can type the following.
Command window
1 > javac MyFirstClass . java
Two things may happen at this point. You may have error messages telling you that the
compiler cannot compile your code because of errors in your Java code. You will need to fix
them before being able to move on.
If the compiler remains silent and does not complain about anything: congratulations! It
means that your Java code has been properly compiled. Checking the content of the directory
again should show a new file in it: [Link]
7 Adding Code to Your Class to Run it
So far your class is empty; there is no executable code in it. If you were able to compile it
properly then you can advance to the next step and execute some code.
Just open your [Link] file and copy the following code in it.
10
HaQT Fundamental Programming
1 public c l a s s MyFirstClass {
3 p u b l i c s t a t i c v o i d main ( S t r i n g . . . a r g s ) {
System . out . p r i n t l n ( ” H e l l o , World ! ” ) ;
5 }
}
As you may know, there is a long-standing tradition in computer science, which is to write
a program that prints ”Hello, World!” on the console of your application. So let us do that!
There is technical code in this class that may not be very clear to you. Do not worry; all
you need to do is to compile it following the steps described in the previous section.
Make sure that the compiler created the [Link] for you. To run it, all you need
to type is the following command:
Command window
> java MyFirstClass
This should print ”Hello, World!” on the console. If this is the case: congratulations! You
have been able to run your first Java program!
8 Common Problems and Their Solutions
Compiler Problems
Common Error Messages on Microsoft Windows Systems
Command window
1 j a v a c i s not r e c o g n i z e d a s an i n t e r n a l o r e x t e r n a l command , o p e r a b l e
program o r batch f i l e
If you receive this error, Windows cannot find the compiler javac.
11
HaQT Fundamental Programming
Here’s one way to tell Windows where to find javac. Suppose you installed the JDK in
C:\jdk20. At the prompt you would type the following command and press Enter:
Command window
1 C: \ j d k 2 0 \ b i n \ j a v a c HelloWorldApp . j a v a
If you choose this option, you’ll have to precede your javac and java commands with C:\jdk20\bin\
each time you compile or run a program. To avoid this extra typing, consult the section
Updating the PATH variable in the JDK 20 installation instructions.
Command window
1 C l a s s names , HelloWorldApp , a r e o n l y a c c e p t e d i f a n n o t a t i o n p r o c e s s i n g i s
e x p l i c i t l y requested
If you receive this error, you forgot to include the .java suffix when compiling the program.
Remember, the command is javac [Link] not javac HelloWorldApp.
Common Error Messages on UNIX Systems
Command window
1 j a v a c : Command not found
If you receive this error, UNIX cannot find the compiler, javac.
Here’s one way to tell UNIX where to find javac. Suppose you installed the JDK in
/usr/local/jdk20. At the prompt you would type the following command and press Re-
turn:
Command window
1 / u s r / l o c a l / j d k 2 0 / j a v a c HelloWorldApp . j a v a
Note: If you choose this option, each time you compile or run a program, you’ll have to
precede your javac and java commands with /usr/local/jdk20/. To avoid this extra typing,
you could add this information to your PATH variable. The steps for doing so will vary
12
HaQT Fundamental Programming
depending on which shell you are currently running.
Command window
1 C l a s s names , ’ HelloWorldApp ’ , a r e o n l y a c c e p t e d i f a n n o t a t i o n p r o c e s s i n g
i s e x p l i c i t l y requested
If you receive this error, you forgot to include the .java suffix when compiling the program.
Remember, the command is javac [Link] not javac HelloWorldApp.
Syntax Errors (All Platforms)
If you mistype part of a program, the compiler may issue a syntax error. The message usually
displays the type of the error, the line number where the error was detected, the code on
that line, and the position of the error within the code. Here’s an error caused by omitting
a semicolon (;) at the end of a statement:
Command window
1 Testing . java : 8 : e r r o r : ’ ; ’ expected
count ++
3 ˆ
1 error
If you see any compiler errors, then your program did not successfully compile, and the
compiler did not create a .class file. Carefully verify the program, fix any errors that you
detect, and try again.
Semantic Errors
In addition to verifying that your program is syntactically correct, the compiler checks for
other basic correctness. For example, the compiler warns you each time you use a variable
that has not been initialized:
13
HaQT Fundamental Programming
Command window
T e s t i n g . j a v a : 8 : e r r o r : v a r i a b l e count might not have been i n i t i a l i z e d
2 count ++ ;
ˆ
4 T e s t i n g . j a v a : 9 : e r r o r : v a r i a b l e count might not have been i n i t i a l i z e d
System . out . p r i n t l n ( ” Input has ” + count + ” c h a r s . ” ) ;
6 ˆ
2 errors
Again, your program did not successfully compile, and the compiler did not create a .class
file. Fix the error and try again.
Runtime Problems
Error Messages on Microsoft Windows Systems
If you receive this error, java cannot find your bytecode file, [Link].
One of the places java tries to find your .class file is your current directory. So if your
.class file is in C:\java, you should change your current directory to that. To change your
directory, type the following command at the prompt and press Enter:
Command window
1 cd c : \ j a v a
The prompt should change to C:\java>. If you enter dir at the prompt, you should see your
.java and .class files. Now enter java HelloWorldApp again.
If you still have problems, you might have to change your CLASSPATH variable. To see if
this is necessary, try clobbering the classpath with the following command.
Command window
1 s e t CLASSPATH=
Now enter java HelloWorldApp again. If the program works now, you’ll have to change your
CLASSPATH variable. To set this variable, consult the Updating the PATH variable section
in the JDK installation instructions. The CLASSPATH variable is set in the same manner.
14
HaQT Fundamental Programming
Command window
1 Could not f i n d o r l o a d main c l a s s HelloWorldApp . c l a s s
A common mistake made by beginner programmers is to try and run the java launcher on the
.class file that was created by the compiler. For example, you’ll get this error if you try to run
your program with java [Link] instead of java HelloWorldApp. Remember,
the argument is the name of the class that you want to use, not the filename.
Command window
1 E x c e p t i o n i n t h r e a d ”main” j a v a . l a n g . NoSuchMethodError : main
The Java VM requires that the class you execute with it have a main method at which to
begin execution of your application. A Closer Look at the Adding Code to Your Class to
Run it section discusses the main method in detail.
Error Messages on UNIX Systems
Command window
1 E x c e p t i o n i n t h r e a d ”main” j a v a . l a n g . NoClassDefFoundError : HelloWorldApp
If you receive this error, java cannot find your bytecode file, [Link].
One of the places java tries to find your bytecode file is your current directory. So, for exam-
ple, if your bytecode file is in /home/jdoe/java, you should change your current directory
to that. To change your directory, type the following command at the prompt and press
Return:
Command window
1 cd /home/ j d o e / j a v a
If you enter pwd at the prompt, you should see /home/jdoe/java. If you enter ls at the
prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.
If you still have problems, you might have to change your CLASSPATH environment variable.
15
HaQT Fundamental Programming
To see if this is necessary, try clobbering the classpath with the following command.
Command window
1 u n s e t CLASSPATH
Now enter java HelloWorldApp again. If the program works now, you’ll have to change your
CLASSPATH variable in the same manner as the PATH variable above.
Command window
1 E x c e p t i o n i n t h r e a d ”main” j a v a . l a n g . NoClassDefFoundError : HelloWorldApp /
class
A common mistake made by beginner programmers is to try and run the java launcher on the
.class file that was created by the compiler. For example, you’ll get this error if you try to run
your program with java [Link] instead of java HelloWorldApp. Remember,
the argument is the name of the class that you want to use, not the filename.
Command window
1 E x c e p t i o n i n t h r e a d ”main” j a v a . l a n g . NoSuchMethodError : main
The Java VM requires that the class you execute with it have a main method at which to
begin execution of your application. A Closer Look at the Adding Code to Your Class to
Run it section discusses the main method in detail.
9 Going Further
This first Java program showed you the basic steps every Java developer follows to run an
application.
1. Create a source in a set of .java text files.
2. Compile these files to produce a set of corresponding .class binary files.
3. Run them together as an application.
16
HaQT Fundamental Programming
Developers that work on large applications do not use plain text editors to manage their
source code; they use Integrated Development Environments. IDEs are complex software
applications, specialized in software development. These applications handle the compilation
of your source code automatically, they can help you to track errors in the syntax of your
Java code and nail down bugs in its execution, among other things.
the Eclipse foundation maintains Eclipse,
the Apache foundation maintains NetBeans,
JetBrains publishes IntelliJ IDEA Community Edition that is free to use for personal
and commercial development.
17