S01L02 – HelloWorld Java program without Maven

html



Creating a HelloWorld Java Project in IntelliJ IDEA Without Maven

Table of Contents

  1. Introduction ....................................................................................................................................1
  2. Setting Up Your Development Environment ..................3
  3. Creating a New Java Project ....................................................................5
  4. Understanding Project Structure ................................................8
  5. Writing Your First Java Program ...................................................10
  6. Running the Application ................................................................................13
  7. Creating a JAR File ..................................................................................................16
  8. Handling Common Issues ................................................................................19
  9. Conclusion ................................................................................................................................22

Introduction

Welcome to this comprehensive guide on creating a HelloWorld Java project in IntelliJ IDEA without Maven. Whether you're a beginner embarking on your programming journey or a developer with basic knowledge seeking to refine your skills, this eBook will walk you through each step meticulously.

Importance and Purpose

Java remains one of the most popular programming languages due to its versatility and robustness. IntelliJ IDEA, a powerful Integrated Development Environment (IDE), enhances the Java development experience. This guide aims to simplify the process of setting up a Java project without the complexities of Maven, allowing you to focus on learning core Java concepts.

Pros and Cons

Pros Cons
Simplicity: Easy to set up for beginners without dependency management. Scalability: Not ideal for larger projects requiring dependency management.
Control: Greater understanding of project structure and configurations. Manual Management: Requires manual handling of libraries and dependencies.
Lightweight: Faster setup compared to projects using build tools like Maven. Limited Features: Lacks advanced features provided by build tools.

When and Where to Use

Creating a Java project without Maven is ideal for small-scale applications, educational purposes, or when you want to understand the foundational aspects of Java project structures. As your projects grow in complexity, integrating build tools like Maven or Gradle becomes beneficial.


Setting Up Your Development Environment

Before diving into project creation, ensure your development environment is adequately set up.

Installing IntelliJ IDEA

  1. Download IntelliJ IDEA: Visit the official website and download the Community edition, which is free and suitable for Java development.
  2. Install IntelliJ IDEA: Follow the installation prompts specific to your operating system.
  3. Launch IntelliJ IDEA: Open the application after installation.

Configuring JDK

  1. Download JDK: If not already installed, download Microsoft Java 17 or any other preferred Java version from the official Oracle website.
  2. Set Up JDK in IntelliJ IDEA:
    • Navigate to File > Project Structure.
    • Under Platform Settings, select SDKs.
    • Click the + icon and choose JDK.
    • Locate the installed JDK directory and confirm.

Table 1: Comparing Java Versions

Feature Java 17 Java 21
Release Date September 2021 September 2023
Long-Term Support (LTS) Yes Yes
Key Features Sealed Classes, Pattern Matching Enhanced Performance, Improved Security

Creating a New Java Project

With your environment set, let's create a new Java project.

Steps to Create a New Project

  1. Open IntelliJ IDEA: On the welcome screen, click New Project.
  2. Select Project Type: Choose Java from the options.
  3. Project SDK: Ensure the selected JDK is the one you configured earlier.
  4. Project Name: Enter HelloWorld.
  5. Project Location: Choose your preferred directory.
  6. Maven Option: Uncheck the Use Maven option to proceed without it.
  7. Finish: Click Finish to create the project.

Diagram 1: New Project Setup in IntelliJ IDEA

New Project Setup


Understanding Project Structure

A well-organized project structure is crucial for maintaining and scaling your application.

Basic Project Structure

Folder Descriptions

  • src/: Contains source code files. Here, HelloWorld.java resides.
  • out/: Stores compiled class files and artifacts like JAR files.
  • META-INF/: Contains metadata about the project, including manifest files.
  • HelloWorld.iml: Module file for IntelliJ IDEA.

Writing Your First Java Program

Let's craft the quintessential "Hello, World!" program.

HelloWorld.java

Explanation

  • Package Declaration: Organizes classes into namespaces. Here, org.studyeasy is the package name.
  • Class Definition: HelloWorld is the class containing the main method.
  • Main Method: Entry point of the Java application. It prints "Hello and welcome!" to the console.

Running the Application

Executing your Java program is straightforward.

Steps to Run

  1. Navigate to Main Class: In the src/org/studyeasy directory, locate HelloWorld.java.
  2. Run the Program:
    • Right-click on HelloWorld.java.
    • Select Run 'HelloWorld.main()'.
  3. View Output: The console will display:

Troubleshooting

If you encounter issues running the application, ensure:

  • The correct JDK is selected.
  • The main method is correctly defined.
  • There are no syntax errors in your code.

Creating a JAR File

A JAR (Java ARchive) file packages your application into a single file for distribution.

Steps to Create a JAR File

  1. Navigate to Project Structure:
    • Go to File > Project Structure.
  2. Add Artifact:
    • In the Project Structure window, select Artifacts.
    • Click the + icon and choose JAR > From modules with dependencies.
  3. Configure JAR:
    • Select the main class (HelloWorld).
    • Ensure Include in project build is checked.
  4. Apply and OK: Click Apply and then OK to save settings.
  5. Build the Artifact:
    • Go to Build > Build Artifacts.
    • Select Build to generate the JAR file.

Table 2: JAR File Locations

Folder Contents
out/artifacts/HelloWorld_jar/ Contains HelloWorld.jar
out/production/HelloWorld/ Contains compiled .class files

Running the JAR File

  1. Locate the JAR: Navigate to out/artifacts/HelloWorld_jar/HelloWorld.jar.
  2. Run via Command Line:
  3. Expected Output:

Handling Exceptions

  • Class Not Found Exception:
    • Ensure the MANIFEST.MF correctly references the main class.
    • Verify that the HelloWorld.class is included in the JAR.

Handling Common Issues

Developing Java applications can present various challenges. Here are solutions to common problems encountered during this process.

Issue 1: JAR File Not Found

Solution:

  • After building the artifact, refresh the project by selecting File > Reload from Disk.
  • Ensure that the build process completes without errors.

Issue 2: Class Not Found Exception

Solution:

  • Confirm that the MANIFEST.MF file specifies the correct main class.
  • Rebuild the artifact to update configurations.

Issue 3: UI Changes in IntelliJ IDEA

Solution:

  • Stay updated with the latest IntelliJ IDEA versions.
  • Explore new UI elements and adjust configurations accordingly.
  • Refer to IntelliJ IDEA's official documentation for guidance.

Diagram 2: Resolving Class Not Found Exception

Class Not Found Exception


Conclusion

Embarking on your Java development journey by creating a HelloWorld project in IntelliJ IDEA without Maven sets a strong foundation. This approach offers a clear understanding of project structures, configurations, and the fundamental concepts of Java programming. As you become more comfortable, integrating tools like Maven can further enhance your development workflow.

Key Takeaways

  • Environment Setup: Properly configure IntelliJ IDEA and JDK to avoid setup issues.
  • Project Structure: Understanding the organization of source and compiled files is crucial.
  • Running Applications: Mastering the run process and handling exceptions ensures smooth development.
  • JAR File Creation: Packaging your application into a JAR file is essential for distribution.

Call to Action

Start by setting up your development environment and create your first Java project today. Experiment with the configurations, explore the IntelliJ IDEA features, and continue building more complex applications as you advance.


Supplementary Information

Differences Between Maven and Manual Project Setup

Feature Without Maven With Maven
Dependency Management Manual handling of libraries Automated via pom.xml
Build Process Manual compilation and packaging Streamlined build lifecycle
Project Scalability Suitable for small projects Ideal for large, complex projects
Configuration More control over configurations Convention over configuration approach

Additional Resources

Share your love