How to Write Java Code in IntelliJ IDEA: A Beginner's Guide to Coding Greatness

IntellijIDEA

How to Write Java Code in IntelliJ IDEA: A Beginner’s Guide to Coding Greatness

Welcome, brave adventurer, to the wild world of Java programming! You’ve decided to code in Java—congratulations, you’re officially on the path to ruling the digital universe. But before you get too carried away with visions of building the next Facebook or Google, let’s talk about IntelliJ IDEA, your new sidekick. It’s like Batman’s Batmobile but for coding. Buckle up!


Step 1: Installing IntelliJ IDEA – Downloading Your Superpower

First thing’s first: you need to install IntelliJ IDEA. Think of this as downloading your coding superpowers. Without it, you’re just Bruce Wayne. But with it? Full-on Batman mode. Ready? Let’s do this.

For Arch Linux users:

Ah, you brave souls. If you’re using Arch Linux, you’re already part of an elite group of tech warriors. Installing software on Arch is like navigating a jungle with only a terminal and sheer determination, but don’t worry—I’ve got your back.

For you, I’ve prepared an installation guide that will not only get you through, but also leave you with a smug smile knowing you’ve done it the Arch way. Because why use a graphical installer when you can have all the control and none of the simplicity, right?

  1. Fly over to JetBrains’ website.
  2. Download the Community Edition. Why? Because it’s free and loaded with everything you need to be awesome. Free = good, right?
  3. Install IntelliJ and marvel at its sleekness. It’s okay if you shed a single tear of joy.

Boom! Now you’re armed with the ultimate weapon in coding: IntelliJ IDEA. You’re ready to code… but don’t worry, we’re not sending you off to battle just yet.


Step 2: Setting Up a Spring Boot Project – Building Your Lair

Sure, you could start with a simple Java project. But why do things the boring way when we can jump straight into Spring Boot and feel like coding superheroes? We’re here for greatness, not mediocrity.

Enter Spring Initializr. This magical tool sets up your Spring Boot project faster than you can say “dependency injection.”

Here’s how to use it:

  1. Go to Spring Initializr
    Imagine this like ordering at a fast-food joint, except instead of fries, you get a fully functional Java project.

    • Project: Choose Maven because it’s the default choice of legends.
    • Language: Java, duh. This isn’t Python land (yet).
    • Spring Boot Version: Stick with the latest version—you’re not here for outdated software, are you?
  2. Customize Your Order
    Let’s fill out your project’s details:

    • Group: Use com.example, because even Java developers love a classic.
    • Artifact: Name it something heroic like EpicSpringApp because, well, you’re epic.
  3. Add Some Spice (Dependencies)
    Choose these must-haves:

    • Spring Web: For building web applications and impressing your future self.
    • Spring Boot DevTools: It’s like auto-refresh for code. Makes you feel like a magician when things change instantly.
  4. Generate That Baby
    Click the Generate button, and your project will zip its way into your downloads faster than a coffee-fueled programmer typing at 3 a.m.

  5. Unzip the Project
    Now unzip it like you’re unwrapping the coolest gift ever—your very own Spring Boot project. No socks in this package, just pure coding joy.


Step 3: Importing the Project into IntelliJ IDEA – Feel Like a Real Developer

With your project downloaded, it’s time to let IntelliJ IDEA do its thing and make you look like the next coding prodigy.

  1. Launch IntelliJ IDEA
    Open up IntelliJ IDEA, and take a deep breath. That’s the smell of endless coding possibilities.

  2. Open Your Project

    • On the welcome screen, click Open.
    • Select your unzipped Spring Boot project folder. IntelliJ will be like, “I see you brought a Maven project. Let me handle everything for you.” Cue applause.
  3. Let IntelliJ Work Its Magic
    IntelliJ will download all your project’s dependencies, and by the time you’re done sipping your coffee (or chugging energy drinks), you’ll be ready to roll.


Step 4: Writing Your First Java Code – Let the Real Fun Begin

Now that everything’s set up, let’s flex those coding muscles and write some Java code. Don’t worry, it’s going to be more fun than you think—like solving a puzzle but with way more bragging rights.

  1. Find Your Main Class
    In the src/main/java/com/example folder, you’ll find a file named EpicSpringAppApplication.java. This file is the chosen one—it’s where Spring Boot kicks off all the action.

  2. Understand the Main Class
    Open that file, and you’ll see some pre-written code that looks like this:

package com.example.epicspringapp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EpicSpringAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(EpicSpringAppApplication.class, args);
    }
}

Translation: “Dear Spring Boot, please start the server and do cool web stuff.” That’s all you need to know for now.


Step 5: Create a REST Controller – Because You’re Fancy Like That

Let’s kick things up a notch and add a REST endpoint. This way, when people (or your browser) visit your app, they’ll be greeted with a “Hello, World!” message—because that’s basically the programming rite of passage.

  1. Create a New Class
    Right-click on src/main/java/com/example/epicspringapp and select New > Java Class. Name it something slick like HelloController. Why? Because we’re about to say hello to the world, my friend.

  2. Write Some Code
    Inside your new HelloController class, type this masterpiece:

package com.example.epicspringapp;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

Congratulations! You’ve just written a REST API. Fancy, huh? This bad boy will respond with “Hello, World!” when someone (or some robot) visits http://localhost:8080/hello.

You’re officially 98% cooler now.


Step 6: Running the Application – Ready for Takeoff

Now, let’s fire up this application and let the world witness your greatness.

  1. Run the App
    Go back to your EpicSpringAppApplication.java file and click the green play button next to the main method, or use the super-secret keyboard shortcut Shift + F10 (you’ll look like a wizard doing this).

  2. Look at the Console
    As your application boots up, IntelliJ will show you a bunch of logs in the Run window. Look for the magic words:

Tomcat started on port(s): 8080

This means your application is up and running! It’s like your app just woke up and said, “I’m ready to party.”

  1. Test Your Endpoint
    Open your web browser, type in http://localhost:8080/hello, and hit Enter. What do you see? That’s right—“Hello, World!” greeted you like a long-lost friend. Give yourself a high-five.

Conclusion

And there you have it! You’ve gone from installing IntelliJ IDEA to writing Java code like a rockstar. Here’s what you’ve accomplished:

  • Installed IntelliJ IDEA and felt like a coding superhero.
  • Set up a Spring Boot project using Spring Initializr, aka the drive-thru of project setups.
  • Created a simple REST API that says “Hello, World!"—because nothing says “I’m a programmer” like that.
  • Ran your application and flexed your newfound skills.

See also