Progress2 of 14 topics

14% complete

Setting Up Your Java Development Environment

Before you can start writing Java code, you need to set up your development environment. This tutorial will guide you through installing the Java Development Kit (JDK), setting up an Integrated Development Environment (IDE), and creating your first Java project.

What You'll Need

Computer

Any modern computer with Windows, macOS, or Linux operating system.

Internet Connection

To download the JDK and IDE.

Disk Space

At least 4GB of free disk space for the JDK and IDE.

Installing the Java Development Kit (JDK)

The JDK is a software development environment used for developing Java applications. It includes the Java Runtime Environment (JRE), an interpreter/loader (Java), a compiler (javac), an archiver (jar), and other tools needed for Java development.

Step 1: Download the JDK

There are several JDK distributions available:

For beginners, we recommend Eclipse Temurin (formerly AdoptOpenJDK) as it's free for any use and well-maintained.

Which Java Version?

For learning purposes, we recommend using the latest Long-Term Support (LTS) version, which is currently Java 21. LTS versions receive updates and support for an extended period.

Step 2: Install the JDK

Windows

  1. Download the Windows installer (.msi or .exe)
  2. Run the installer and follow the prompts
  3. The installer will set up the JDK and add Java to your PATH

macOS

  1. Download the macOS installer (.pkg or .dmg)
  2. Open the installer and follow the prompts
  3. The JDK will be installed in /Library/Java/JavaVirtualMachines/

Linux

  1. Download the Linux package (.tar.gz or use package manager)
  2. Extract the archive to a directory of your choice
  3. Add the bin directory to your PATH

Step 3: Verify the Installation

Open a terminal or command prompt and run the following commands:

bash
1# Check Java version
2java -version
3
4# Check Java compiler version
5javac -version

You should see output similar to this:

bash
1openjdk version "21" 2023-09-19
2OpenJDK Runtime Environment Temurin-21+35 (build 21+35)
3OpenJDK 64-Bit Server VM Temurin-21+35 (build 21+35, mixed mode)
4
5javac 21

Setting Up an Integrated Development Environment (IDE)

While you can write Java code in any text editor and compile it using the command line, an IDE makes Java development much more efficient with features like code completion, debugging tools, and project management.

Popular Java IDEs

IDEProsBest For
IntelliJ IDEAPowerful features, smart code completion, excellent refactoring toolsProfessional development, all project sizes
EclipseFree, extensive plugin ecosystem, highly customizableEnterprise development, plugin development
Apache NetBeansFree, easy to use, good for beginnersLearning, small to medium projects
Visual Studio CodeLightweight, fast, extensive extensionsSmall projects, multi-language development

For this tutorial, we'll use IntelliJ IDEA Community Edition, which is free and provides an excellent balance of features for beginners and professionals alike.

Installing IntelliJ IDEA

  1. Go to the IntelliJ IDEA download page
  2. Download the Community Edition (free)
  3. Run the installer and follow the prompts
  4. During installation, select the options that best suit your needs (default options are usually fine)

Creating Your First Java Project

Now that you have the JDK and IDE installed, let's create your first Java project.

Using IntelliJ IDEA

  1. Launch IntelliJ IDEA
  2. Click on "New Project" on the welcome screen
  3. Select "Java" from the left panel
  4. Make sure the JDK you installed is selected in the "Project SDK" dropdown
  5. Click "Next"
  6. You can select "Create project from template" and choose "Command Line App" for a simple starter project
  7. Click "Next"
  8. Enter a project name (e.g., "HelloJava") and location
  9. Click "Finish"

IntelliJ will create a new project with a basic structure and a Main.java file.

Writing Your First Java Program

Let's modify the Main.java file to create a simple "Hello, World!" program:

java
1public class HelloWorld {
2 public static void main(String[] args) {
3 // This is a simple Java program that prints a message to the console
4 System.out.println("Hello, World!");
5
6 // Variables and basic operations
7 int a = 10;
8 int b = 20;
9 int sum = a + b;
10
11 System.out.println("Sum of " + a + " and " + b + " is " + sum);
12
13 // Using String formatting (similar to printf in C)
14 System.out.printf("Sum of %d and %d is %d
15", a, b, sum);
16 }
17}

Running Your Java Program

In IntelliJ IDEA, you can run your program in several ways:

  • Click the green "Run" button in the gutter next to the main method
  • Right-click anywhere in the editor and select "Run 'Main.main()'"
  • Use the keyboard shortcut: Shift+F10 (Windows/Linux) or Control+R (macOS)

You should see the output in the "Run" tool window at the bottom of the IDE:

plaintext
1Hello, World!
2Sum of 10 and 20 is 30
3Sum of 10 and 20 is 30
4
5Process finished with exit code 0

Understanding Java Project Structure

A typical Java project has the following structure:

MyJavaProject/
  ├── src/                  # Source code
  │   └── com/example/      # Package structure
  │       └── Main.java     # Java source file
  ├── out/                  # Compiled bytecode
  │   └── production/
  │       └── MyJavaProject/
  │           └── com/example/
  │               └── Main.class
  └── lib/                  # External libraries

In Java, code is organized into packages, which are hierarchical namespaces for your classes. The convention is to use reverse domain name notation (e.g., com.example.myproject).

Java Build Tools

For simple projects, you can use the built-in compilation tools in your IDE. For larger projects, you might want to use a build tool like:

Maven

A popular build automation tool used primarily for Java projects. It uses a pom.xml file to define project dependencies and build process.

Gradle

A more modern build tool that uses a Groovy or Kotlin-based DSL instead of XML. It's more flexible and often faster than Maven.

Ant

An older build tool that uses XML build files. It's less common for new projects but still used in some legacy systems.

For beginners, you don't need to worry about these build tools yet. Your IDE will handle the compilation process for you.

Troubleshooting Common Setup Issues

IssuePossible Solution
'java' is not recognized as a commandJava is not in your PATH. Add the JDK's bin directory to your system's PATH environment variable.
No JDK found when creating a projectIn your IDE, manually configure the JDK path in the project settings or preferences.
Compilation errors in a simple programCheck for typos, missing semicolons, or incorrect capitalization. Java is case-sensitive.
IDE is slow or unresponsiveIncrease the memory allocation for the IDE in its configuration file, or close unused projects.

Practice Exercises

Try these exercises to reinforce your Java setup knowledge:

  1. Create a new Java project with a different name
  2. Write a program that prints your name and age
  3. Modify the program to calculate and print your age in days
  4. Create a new class in your project and call a method from it in your main class
  5. Export your project as a JAR file and run it from the command line

Summary

In this tutorial, you've learned:

  • How to install the Java Development Kit (JDK)
  • How to set up IntelliJ IDEA as your Java IDE
  • How to create and run your first Java project
  • The basic structure of a Java project
  • Common build tools used in Java development
  • How to troubleshoot common setup issues

With your development environment set up, you're ready to start learning Java programming. In the next tutorial, we'll explore Java variables and data types to begin writing more complex code.

Related Tutorials

Learn the basics of Java and why it's important.

Learn more

Learn about different types of data and how to store them in Java.

Learn more

Learn about decision making and loops in Java.

Learn more