- How to write, compile and run a hello world Java program for beginners
- 1. Download and install Java Development Kit
- 2. Set up environment variables
- 3. Code a Java hello world program
- 4. Compile your first Java program
- 5. Run your first Java program
- 6. What we have learnt so far
- Related Java Hello World Tutorials:
- About the Author:
- How to create, build and run a Java Hello World program with Eclipse
- 1. Download and Install Eclipse IDE
- 2. Choose a Workspace Directory
- 3. Change Perspective
- 4. Create a Java Project
- 5. Write Your First Java Program
- 6. Compile, Build and Run Your First Java Program
- Related Java Hello World Tutorials:
- Other Eclipse Tutorials:
- About the Author:
How to write, compile and run a hello world Java program for beginners
If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have come to the right place. This tutorial will help you writing your first Java program, typically a “hello world” one — your first step of the adventure into Java programming world. Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.
To start, all you need is a fresh computer without any Java software installed, a text-based editor and a good internet connection.
NOTES: This beginner tutorial is targeted for Windows environment.
1. Download and install Java Development Kit
In order to write and run a Java program, you need to install a software program called Java SE Development Kit (or JDK for short, and SE means Standard Edition). Basically, a JDK contains:
-
- JRE(Java Runtime Environment): is the core of the Java platform that enables running Java programs on your computer. The JRE includes JVM(Java Virtual Machine) that runs Java programs by translating from bytecode to platform-dependent code and executes them (Java programs are compiled into an intermediate form called bytecode), and other core libraries such as collections, File I/O, networking, etc.
- Tools and libraries that support Java development.
-
- javac.exe : is Java compiler that translates programs written in Java code into bytecode form.
- java.exe : is the Java Virtual Machine launcher that executes bytecode.
Check the option “Accept License Agreement”, and choose an appropriate version for your computer from the list. Here we choose the version for Windows x64:
After downloading the program, run it to install the JDK on your computer (just following the steps, leave the defaults and click Next, Next…):
You would see the JDK is installed in the following directory, for example: C:\Program Files\Java\jdk1.7.0_21. The following screenshot describes the JDK’s directory structure:
Now let’s test if Java runtime is installed correctly. Open a command prompt window and type:
java -version
You would see the following result:
That shows version of the JRE, e.g. “1.7.0_21” — Congratulations! Your computer is now capable of running Java programs.
Now try to type the following command:
javac -version
You would see the following error:
That’s because Windows could not find the javac program, so we need to set some environment variables which tell the location of javac.exe .
2. Set up environment variables
Now we’re going to set environment variables so that the javac.exe program can be accessed anywhere from command line. On Windows 7, go to My Computer and click System Properties:
Then click Advanced system settings:
The System Properties dialog appears, select Advanced tab and click Environment Variables. :
The Environment Variable dialog appears, click on the New… button under the System variables section.
That opens up the New System Variable dialog. Type the following information:
The field Variable name must be JAVA_HOME , and the field Variable value must point to JDK’s installation directory on your computer. Here it is set to c:\Program Files\Java\jdk1.7.0_21. Click OK to close this dialog.
Now back to the Environment Variables dialog, look for a variable called Path under the System Variables list, and click Edit…:
In the Edit System Variable dialog, append the following to the end of the field Variable value:
;%JAVA_HOME%\bin
Note that there is a semicolon at the beginning to separate this value from other ones. Click OK three times to close all the dialogs.
Now we have to quit the current command prompt and open a new one to see the changes takes effect. Type the following command again in the re-opened command prompt window:
javac -version
You would see the following output:
Congratulations! You have completed the setup for essential Java development environment on your computer. It’s now ready to write your first Java program.
3. Code a Java hello world program
Save the file as HelloWorld.java (note that the extension is .java ) under a directory, let’s say, C:\Java.
Don’t worry if you don’t understand everything in this simple Java code. The following picture explains it nicely:
Every Java program starts from the main() method. This program simply prints “Hello world” to screen.
4. Compile your first Java program
Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored:
And type the following command:
javac HelloWorld.java
That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file name ends with .java extension. You would see the following output:
If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command line, we’ll see the .class file:
So remember a Java program will be compiled into bytecode form (.class file).
5. Run your first Java program
java HelloWorld
That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension). You would see the following output:
It just prints out “Hello world!” to the screen and quits. Congratulations! You have successfully run your first Java program!
6. What we have learnt so far
-
- JDK is the Java SE Development Kit that contains tools and libraries for Java development.
- JRE is the Java Runtime Environment that enables running Java programs on your computer.
- JVM is the Java Virtual Machine that actually executes Java programs. With JVM, programs written in Java can run on multi-platforms (thus Java is called cross-platform language).
- How to install JDK and configure environment variables.
- javac is the Java compiler. It translates Java source code into bytecode.
- java is the JVM launcher which we use to run our program.
- Every Java program starts from the main() method.
- When compiling, the compiler generates a .class file from a .java file.
You can also watch the video version of this tutorial:
Next, I recommend you to read this article: Understand Classes and Objects in Java
Related Java Hello World Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.
How to create, build and run a Java Hello World program with Eclipse
If you are new to Java programming and Eclipse IDE, this step-by-step tutorial helps you get started to be familiar with the most Java IDE by writing your first Java program using Eclipse. And you will be able to build and run your program inside Eclipse.
1. Download and Install Eclipse IDE
Eclipse is the most popular Integrated Development Environment (IDE) for developing Java applications. It is robust, feature-rich, easy-to-use and powerful IDE which is the #1 choice of almost Java programmers in the world. And it is totally FREE.
As of now (fall 2016), the latest release of Eclipse is Neon (version 4.6). Click the following link to download Eclipse:
You will see the download page like this:
You can install Eclipse either by downloading the Eclipse Installer or package (zip file). I’d recommend you to download by package. Eclipse comes with various packages for different development purposes. For Java, there are two main packages listed as you see above:
- Eclipse IDE for Java EE Developers: This is for developing Java EE applications (web applications using Servlets & JSP).
- Eclipse IDE for Java Developers: This is for developing Java SE applications, a subset of the Java EE Developer package.
Click on the link 32-bit or 64-bit (depending on the bit version of your operating system) to start download the package.
Extract this ZIP file into a directory on your computer. You will see a directory called eclipse containing Eclipse’s installed files:
Eclipse Neon requires Java 8 or newer so make sure you have JDK 8 already installed on your computer. If not, follow this tutorial to install JDK.
Click eclipse.exe file (Windows) to start the IDE. You will see the splash screen of Eclipse Neo:
That’s it! You have successfully installed Eclipse IDE. Next, let’s see how to create a workspace.
2. Choose a Workspace Directory
Eclipse organizes projects by workspaces. A workspace is a group of related projects and it is actually a directory on your computer. That’s why when you start Eclipse, it asks to choose a workspace location like this:
By default, Eclipse created a workspace directory at your USER_HOME\workspace. If you want to choose another directory, click Browse. Here I chose a different workspace:
Check Use this as the default and do not ask again if you don’t want to be asked whenever you start Eclipse. You can always change workspace when Eclipse is running.
Click OK. You should see the welcome screen:
Now, we are ready to create a Java project.
3. Change Perspective
Before creating a new Java project, let familiarize yourself with Perspective. Imagine a perspective is a predefined configuration specialized for a specific aspect of development process such as Java, Java EE, Debug, Database Development, Web, etc. Depending on your need, you can switch back and forth among different perspectives during a development session.
Since we installed Eclipse IDE for Java EE Developers, the default perspective is Java EE. To change perspective, go to Window > Perspective > Open Perspective > Other… You will see a small dialog listing all available perspectives:
Here we choose Java perspective. Click OK. Here’s how the Java perspective would look like:
4. Create a Java Project
To create a new Java project in Eclipse, go to File > New > Java Project. The New Java Project wizard dialog appears let you specify configurations for the project:
Enter project name: HelloWorld. Leave the rest as it is, and click Finish.
You should see the HelloWorld project is created in the Package Explorer view as following:
It’s recommended to create a package for your project. Right click on the project, and select New > Package from the context menu:
In the New Java Package dialog, enter the name your package. Here I enter net.codejava :
Click Finish. You should see the newly created package appears:
Now, it’s time to create a Java class for your hello world application.
5. Write Your First Java Program
To create a new Java class under a specified package, right click on the package and select New > Class from the context menu:
The New Java Class dialog appears, type the name of class as HelloWorld and choose the option to generate the main() method:
And click Finish. The HelloWorld class is generated like this:
Now, type some code in the main() method to print the message “Hello World” to the console:
That’s it. We have created a Java hello world program using Eclipse IDE.
6. Compile, Build and Run Your First Java Program
By default, Eclipse compiles the code automatically as you type. And it will report compile errors in the Problems view at the bottom like this:
If you want to disable automatically build feature, click the menu Project and uncheck Build Automatically:
However, it’s strongly recommended to keep the auto build mode for it helps you detect errors instantly.
Now, let’s run the hello world application. Click menu Run > Run (or press Ctrl + F11), Eclipse will execute the application and show the output in the Console view:
That’s it! The HelloWorld program has run and printed the output “Hello World” and terminates.
We hope this tutorial help you get started with Eclipse and Java easily. Thank you for reading.
What’s next? I recommend you to continue with this article: Java OOP: Understand Classes and Objects
Watch this tutorial in video:
Related Java Hello World Tutorials:
Other Eclipse Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.