Lesson 6 / 140

Lesson access পরীক্ষা করা হচ্ছে...

Video এবং লিখিত lesson · 18 মিনিট

Creating Your First Java Project

Create a Java project in IntelliJ IDEA, add a class with a `main` method, run it, and read the console output and exit code.

PreviewCaptionsশীঘ্রই

আপনি একটি free preview lesson দেখছেন।

Lesson Overview

In the previous lesson, we installed OpenJDK 21 (the Eclipse Temurin build) and configured IntelliJ IDEA. Now we will create our first Java project using IntelliJ IDEA, write a class with a main method, and run it.

Every program in this course lives in a project, which keeps your source files together and tells the IDE which JDK to use. By the end of the lesson, you will have a working project for the upcoming lessons.

Learning Objectives

After completing this lesson, you will be able to:

  • Create a Java project in IntelliJ IDEA
  • Configure the project name, location, and JDK
  • Understand the purpose of the src directory
  • Create a Java class
  • Write a basic main method
  • Run a Java program from IntelliJ IDEA
  • Understand console output and exit codes
  • Identify common project setup problems

What Is a Java Project?

A Java project is a structure used to organize related Java source code and project configuration. A simple project may look like this:

text
first-java-project/
└── src/
    └── Main.java

We will keep our Java source code inside the src directory. The structure of a larger application can be more complex. We will learn about build tools, test structure, resources, and dependency management later when needed.

Creating Your First Java Project

Open IntelliJ IDEA and select New Project from the Welcome screen. If a project is already open, you can create a new project from the menu:

text
File
→ New
→ Project

The interface may look slightly different depending on your IntelliJ IDEA version.

Step 1: Select Java

Select Java as the project type or language.

Step 2: Enter the Project Name

Enter first-java-project as the project name. It is a good practice to keep project names short and meaningful, such as java-foundation, grade-calculator or oop-practice.

Step 3: Select the Project Location

Store the project in a directory that is easy to find and where you have read and write permissions, such as Documents/java-projects/first-java-project or Development/java/first-java-project. Avoid storing your project inside system directories.

Step 4: Select the JDK

Select the Temurin 21 entry as the JDK, for example temurin-21 (the exact label depends on your IntelliJ IDEA version). All projects in this course will use Java 21. If it is not listed, select Add JDK and choose the JDK 21 folder that you installed in the previous lesson, or use Download JDK with version 21 and vendor Eclipse Temurin.

Step 5: Select the Build System

Select IntelliJ as the build system. At this stage, we are not using Maven or Gradle. A simple IntelliJ project helps us focus on the Java language and programming fundamentals.

Step 6: Create the Project

If the wizard shows an Add sample code option, clear it. In this lesson, we will create the Main class ourselves. Conceptually, the configuration should look like this:

text
Name: first-java-project
Language: Java
Build System: IntelliJ
JDK: temurin-21

Then select Create.

Project Structure

After creating the project, you will see the project's files and directories in the Project panel on the left side: the first-java-project folder with an empty src directory inside. IntelliJ IDEA may create additional configuration or generated directories. We do not need to worry about them yet. The most important directory at this stage is src.

What Is the src Directory?

src is the source directory. The Java source code we write will be stored inside this directory:

text
first-java-project/
└── src/
    ├── Main.java
    ├── Student.java
    └── GradeCalculator.java

IntelliJ IDEA uses src as a source root: it compiles, analyzes and runs the Java code inside it, and shows errors as you type. In this lesson, we will have only one class.

Creating Your First Java Class

In the Project panel, right-click the src directory and select:

text
New
→ Java Class

Enter the class name Main. IntelliJ IDEA will create Main.java, and the initial code may look like:

java
public class Main {
}

Class and File Name

Our class is named Main, so the file name will be Main.java. Java is case-sensitive, so Main and main are not the same name.

Java class names generally follow the PascalCase naming convention: each word starts with a capital letter, as in Main, Student, GradeCalculator and BankAccount.

Adding the main Method

Now write the following inside the Main class:

java
public class Main {

    public static void main(String[] args) {

    }
}

A basic Java application usually starts execution from this main method. There are several new keywords in this signature:

java
public static void main(String[] args)

You do not need to memorize or deeply understand them yet. In the next lesson, we will understand each part of the program separately.

Writing Your First Output

Inside the main method, write System.out.println("Hello Java");. The complete program:

java
public class Main {

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

System.out.println(...) displays output in the console. When the program runs, we will get:

text
Hello Java

Running the Program

IntelliJ IDEA usually shows a Run icon next to the main method. Click the Run icon and select Run 'Main.main()'. You can also right-click Main.java and use the same Run option.

Console Output

If the program runs successfully, you will see the following in the Run window:

text
Hello Java

You may also see a message such as:

text
Process finished with exit code 0

What Does Exit Code 0 Mean?

A process exit code can be used to tell the operating system how a program finished. In general, exit code 0 means the program finished normally.

However, exit code 0 does not mean that the program's logic is necessarily correct. It only indicates that the process did not terminate abnormally.

Modify the Program and Run It Again

Change the code to:

java
public class Main {

    public static void main(String[] args) {
        System.out.println("Welcome to Java");
        System.out.println("My first Java project is running");
    }
}

Run it again. Output:

text
Welcome to Java
My first Java project is running

There are two System.out.println(...) statements, so two separate lines are printed.

Project SDK

The JDK you chose in Step 4 is the project's Project SDK: the JDK that IntelliJ IDEA uses to compile and run it. For our project it is temurin-21. If the project does not run or uses the wrong Java version, check the Project SDK first.

Your First Complete Program

Now write the following in Main.java:

java
public class Main {

    public static void main(String[] args) {
        System.out.println("Java and OOP Foundation");
        System.out.println("My first Java project is running.");
        System.out.println("I am ready to learn Java.");
    }
}

Expected output:

text
Java and OOP Foundation
My first Java project is running.
I am ready to learn Java.

If the program runs successfully, your first Java project has been created and configured.

Project Creation Checklist

Before moving to the next lesson, make sure:

  • You created a Java project in IntelliJ IDEA
  • You selected the Temurin 21 JDK as the Project SDK
  • You found the src directory
  • You created the Main class in Main.java
  • You wrote the standard main method with a System.out.println statement
  • You ran the program and received the expected output

Common Mistakes

Project SDK Is Not Configured or Wrong

You may see the message Project SDK is not defined (the wording can differ between IntelliJ IDEA versions), or the project may use a JDK other than Java 21. In both cases, select the Temurin 21 JDK as the Project SDK in the project settings.

Run Option Is Not Visible

IntelliJ IDEA only offers Run for a class with the standard main method, public static void main(String[] args), spelled exactly like that. Also make sure that the file is inside the src directory, the Project SDK is configured, and the code has no compilation errors (red underlines).

Missing Semicolon

Wrong:

java
System.out.println("Hello Java")

Correct:

java
System.out.println("Hello Java");

If a required semicolon is missing at the end of a Java statement, the compiler will report an error.

Class Name and File Name Do Not Match

public class Main must be in Main.java, with the same capitalization. Renaming the file to main.java, or the class to main, makes the compiler report an error, because Java is case-sensitive and Main and main are different names.

Practice Exercise 1: Create a Project

Solutions: lesson 1.22.

Create a project in IntelliJ IDEA:

text
Project name: java-foundation
JDK: temurin-21
Class name: Main

Main.java:

java
public class Main {

    public static void main(String[] args) {
        System.out.println("Java Foundation project created");
    }
}

Expected output:

text
Java Foundation project created

Practice Exercise 2: Modify the Output

Modify the program to print the following information on separate lines:

  • Your name
  • Course name
  • Java version
  • One goal for learning Java

For example:

text
Name: Sakib
Course: Java and OOP Foundation
Java: 21
Goal: Learn backend development

Use your own information.

Practice Exercise 3: Fix the Error

Fix the error in the following code:

<!-- error -->
java
public class Main {

    public static void main(String[] args) {
        System.out.println("Hello Java")
        System.out.println("My first project");
    }
}

Expected output:

text
Hello Java
My first project

Knowledge Check

Question 1

What is a Java project?

Question 2

What is the purpose of the src directory?

Question 3

Which JDK are we using for projects in this course?

Question 4

If there is a public class named Main, what should the source file be named?

Question 5

Which method does a basic Java application usually start from?

Question 6

Which statement did we use to display text in the console?

Question 7

What does exit code 0 generally mean?

Question 8

What should you check if the Run option is not visible?

Knowledge Check Answers

Answer 1

A Java project is a structure used to organize related Java source code and project configuration.

Answer 2

Java source code is stored inside the src directory. IntelliJ IDEA uses it as a source root to compile, analyze, and run the code.

Answer 3

In this course, we are using OpenJDK 21, the Eclipse Temurin build.

Answer 4

The file should be named Main.java.

Answer 5

A basic Java application usually starts execution from:

java
public static void main(String[] args)

Answer 6

We used System.out.println("...");.

Answer 7

Exit code 0 generally means that the program finished normally.

Answer 8

Check whether:

  • A valid main method exists
  • The file is inside the src source directory
  • JDK 21 (Temurin) is configured as the Project SDK
  • There are any compilation errors

Lesson Summary

  • A Java project organizes related Java source code and project configuration.
  • We created first-java-project in IntelliJ IDEA with OpenJDK 21 (Temurin) as the JDK and IntelliJ as the build system.
  • Java source code goes inside the src directory, which IntelliJ IDEA uses as a source root.
  • A public class named Main must be stored in a file named Main.java.
  • A basic Java application starts from the main method, and System.out.println(...) displays output in the console.
  • Exit code 0 means the program finished normally, not that its logic is correct.
  • If the project does not run or uses the wrong Java version, check the Project SDK first.

আগে ভাবুন

শীঘ্রই আসছে

Checkpoint প্রশ্ন, যেখানে lesson উত্তর দেখানোর আগে আপনাকে একটি উত্তর বেছে নিতে হবে।