Programming and Java Fundamentals
Creating Your First Java Project
You are viewing a free preview lesson.
Lesson Overview
In the previous lesson, we configured Azul Zulu JDK 21 and IntelliJ IDEA.
Now we will create our first Java project using IntelliJ IDEA.
In this lesson, you will:
- Create a Java project
- Use Azul Zulu 21 as the Project SDK
- Understand the project structure
- Create a Java class
- Write a
mainmethod - Run the program
- Read console output
By the end of the lesson, you will have a working Java project that you can use for practice in 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
srcdirectory - Create a Java class
- Write a basic
mainmethod - 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:
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.
From the Welcome screen, select:
New Project
If a project is already open, you can create a new project from the menu:
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.
Examples:
java-foundation
grade-calculator
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.
Example:
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:
Azul Zulu 21
as the JDK.
All projects in this course will use Java 21.
If Azul Zulu 21 is not listed, select:
Add JDK
and choose the Azul Zulu JDK 21 directory that you installed in the previous lesson.
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
Conceptually, the configuration should look like this:
Name: first-java-project
Language: Java
Build System: IntelliJ
JDK: Azul Zulu 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.
A simple structure may look like:
first-java-project/
└── src/
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.
first-java-project/
└── src/
├── Main.java
├── Student.java
└── GradeCalculator.java
IntelliJ IDEA uses src as a source root, which allows it to:
- Compile the Java code inside it
- Analyze the code
- Detect errors
- Run the code
In this lesson, we will have only one class.
Creating Your First Java Class
In the Project panel, right-click the:
src
directory.
Select:
New
→ Java Class
Enter the class name:
Main
IntelliJ IDEA will create:
Main.java
The initial code may look like:
public class Main {
}
Class and File Name
Our class is named:
Main
Therefore, the file name will be:
Main.java
public class Main {
}
Java is case-sensitive.
So:
Main
and:
main
are not the same name.
Java class names generally follow the PascalCase naming convention.
Examples:
Main
Student
GradeCalculator
BankAccount
Adding the main Method
Now write the following inside the Main class:
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:
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");
Complete program:
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:
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:
Hello Java
You may also see a message such as:
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:
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:
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 that IntelliJ IDEA uses to compile and run a project is called the Project SDK.
Our project configuration should be:
Project SDK: Azul Zulu 21
If the project does not run or uses the wrong Java version, check the Project SDK first.
Common Project Problems
Project SDK Is Not Configured
Possible message:
Project SDK is not defined
From the project settings, select:
Azul Zulu 21
Run Option Is Not Visible
Check whether the class contains the standard main method:
public static void main(String[] args)
Also make sure:
- The file is inside the
srcdirectory - The Project SDK is configured
- There are no compilation errors in the code
Missing Semicolon
Wrong:
System.out.println("Hello Java")
Correct:
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 and File Name Do Not Match
If the code is:
public class Main {
}
then the source file should be:
Main.java
Wrong Capitalization
Java is case-sensitive.
Class:
public class Main {
}
Therefore, Main and main are different identifiers.
Wrong JDK Selected
If the project is using a JDK other than Java 21, check the Project SDK.
For this project, use:
Azul Zulu 21
Your First Complete Program
Now write the following in Main.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:
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 Azul Zulu 21 as the Project SDK
- You found the
srcdirectory - You created the
Mainclass - The
Main.javafile was created - You wrote the standard
mainmethod - You used
System.out.println - You successfully ran the program
- You received the expected output
Practice Exercise 1: Create a Project
Create a project in IntelliJ IDEA:
Project name: java-foundation
JDK: Azul Zulu 21
Class name: Main
Main.java:
public class Main {
public static void main(String[] args) {
System.out.println("Java Foundation project created");
}
}
Expected output:
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
Example:
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:
public class Main {
public static void main(String[] args) {
System.out.println("Hello Java")
System.out.println("My first project");
}
}
Expected output:
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 Azul Zulu JDK 21.
Answer 4
The file should be named:
Main.java
Answer 5
A basic Java application usually starts execution from:
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
mainmethod exists - The file is inside the
srcsource directory - Azul Zulu 21 is configured as the Project SDK
- There are any compilation errors
Lesson Summary
In this lesson, we learned:
- How to create a Java project in IntelliJ IDEA
- That we are using Azul Zulu 21 as the Project SDK
- Java source code is stored inside the
srcdirectory - A Java class can be created using
New → Java Class - A public class and its source file name must match
- Java class names generally follow PascalCase
- A basic Java application starts from the
mainmethod System.out.printlndisplays output in the console- Java programs can be run from IntelliJ IDEA
- Exit code
0indicates normal process completion - The Project SDK,
mainmethod, and source directory are important when diagnosing common setup problems