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:
- Oracle JDK: The official JDK from Oracle (requires a license for commercial use)
- Eclipse Temurin (AdoptOpenJDK): Free, open-source JDK distribution
- Azul Zulu: Another free, open-source JDK distribution
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
- Download the Windows installer (.msi or .exe)
- Run the installer and follow the prompts
- The installer will set up the JDK and add Java to your PATH
macOS
- Download the macOS installer (.pkg or .dmg)
- Open the installer and follow the prompts
- The JDK will be installed in /Library/Java/JavaVirtualMachines/
Linux
- Download the Linux package (.tar.gz or use package manager)
- Extract the archive to a directory of your choice
- Add the bin directory to your PATH
Step 3: Verify the Installation
Open a terminal or command prompt and run the following commands:
1# Check Java version2java -version34# Check Java compiler version5javac -version
You should see output similar to this:
1openjdk version "21" 2023-09-192OpenJDK Runtime Environment Temurin-21+35 (build 21+35)3OpenJDK 64-Bit Server VM Temurin-21+35 (build 21+35, mixed mode)45javac 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
IDE | Pros | Best For |
---|---|---|
IntelliJ IDEA | Powerful features, smart code completion, excellent refactoring tools | Professional development, all project sizes |
Eclipse | Free, extensive plugin ecosystem, highly customizable | Enterprise development, plugin development |
Apache NetBeans | Free, easy to use, good for beginners | Learning, small to medium projects |
Visual Studio Code | Lightweight, fast, extensive extensions | Small 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
- Go to the IntelliJ IDEA download page
- Download the Community Edition (free)
- Run the installer and follow the prompts
- 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
- Launch IntelliJ IDEA
- Click on "New Project" on the welcome screen
- Select "Java" from the left panel
- Make sure the JDK you installed is selected in the "Project SDK" dropdown
- Click "Next"
- You can select "Create project from template" and choose "Command Line App" for a simple starter project
- Click "Next"
- Enter a project name (e.g., "HelloJava") and location
- 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:
1public class HelloWorld {2 public static void main(String[] args) {3 // This is a simple Java program that prints a message to the console4 System.out.println("Hello, World!");56 // Variables and basic operations7 int a = 10;8 int b = 20;9 int sum = a + b;1011 System.out.println("Sum of " + a + " and " + b + " is " + sum);1213 // Using String formatting (similar to printf in C)14 System.out.printf("Sum of %d and %d is %d15", 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:
1Hello, World!2Sum of 10 and 20 is 303Sum of 10 and 20 is 3045Process 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
Issue | Possible Solution |
---|---|
'java' is not recognized as a command | Java is not in your PATH. Add the JDK's bin directory to your system's PATH environment variable. |
No JDK found when creating a project | In your IDE, manually configure the JDK path in the project settings or preferences. |
Compilation errors in a simple program | Check for typos, missing semicolons, or incorrect capitalization. Java is case-sensitive. |
IDE is slow or unresponsive | Increase 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:
- Create a new Java project with a different name
- Write a program that prints your name and age
- Modify the program to calculate and print your age in days
- Create a new class in your project and call a method from it in your main class
- 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 moreLearn about different types of data and how to store them in Java.
Learn moreLearn about decision making and loops in Java.
Learn more