Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Running Java Programs: Command Line & IDE

Recommended resources

In our last session, we established the theoretical foundation of the Java platform, exploring the distinct roles of the JDK, JRE, and JVM. You now have a clear mental model of how Java achieves its "Write Once, Run Anywhere" promise through the process of compiling source code into bytecode, which is then executed by a virtual machine.

This lesson transitions from theory to practice. We will now guide your "interns" through their first coding task: writing, compiling, and running a simple Java program. Fulfilling this learning outcome is a pivotal moment in any developer's journey. We will cover two essential workflows: the traditional command-line process, which reinforces the concepts from our previous discussion, and the modern IDE-based process, which introduces the productive environment they will use for the Smart Warehouse project.

The Fundamental Workflow: Command-Line Compilation and Execution

Before diving into the conveniences of an Integrated Development Environment (IDE), it is crucial for a new developer to understand the fundamental steps involved in bringing a program to life. This hands-on, manual process directly connects to the roles of the compiler (javac) from the JDK and the launcher (java) from the JRE.

For your interns, you can frame this as "looking under the hood" to see what the IDE will eventually automate for them. This knowledge is invaluable for troubleshooting and deepens their understanding of the Java ecosystem.

Writing the First Java Program

A Java program begins as a simple text file with a .java extension. The core of this file is a class. A class acts as a blueprint for objects, but for now, we can think of it as a container for our program's code.

Every standalone Java application needs an entry point—a specific place where the JVM knows to begin execution. This is the main method, which has a very specific signature: public static void main(String[] args).

Let's assemble a basic program. This program will simply print a message to the console, confirming that the system is running. We'll call our class WarehouseSystem to align with the project.

public class WarehouseSystem {
    public static void main(String[] args) {
        System.out.println("Smart Warehouse System: Online");
    }
}

A quick breakdown for your interns:

  • public class WarehouseSystem: Declares a class named WarehouseSystem. The file must be saved as WarehouseSystem.java.
  • public static void main(String[] args): The main entry point method.
  • System.out.println(...): A standard library command to print a line of text to the console.

The Two-Step Process: Compile and Run

Once the .java file is saved, the two-step process begins:

  1. Compile: Use the Java compiler (javac) to transform the human-readable WarehouseSystem.java file into platform-independent bytecode. This creates a new file, WarehouseSystem.class.
  2. Execute: Use the Java application launcher (java) to tell the JVM to load and run the code contained within the WarehouseSystem.class file.

The following image perfectly illustrates this workflow, showing the source code in a text editor on the left and the sequence of commands in the command prompt on the right.

A visual representation of the two-step process: writing Java code in a text editor (left), then using the `javac` command to compile it and the `java` command to execute it in the command prompt (right).

To see this process in action from start to finish, the following video provides a clear, step-by-step walkthrough. It covers creating the file, writing the code, compiling, and running, all from the command prompt.

How to Run Java Programs With Command Prompt (cmd) and Notepad in Windows

This video from Coding with John demonstrates the entire command-line workflow on Windows. It's a practical guide that your interns can follow directly.

Pay attention to these key steps: Verifying the JDK installation using java -version. Creating and renaming the file to have a .java extension. Writing the basic Java code in Notepad. Compiling the code with javac, which generates the .class file. Running the program with the java command.

To supplement the video, the resource below provides a textual explanation of the command-line tools and the compilation process.

Compilation, Interpretation and Execution process in Java

This article from OpenGenus IQ details the compilation and execution steps. The section we're interested in explicitly shows the javac and java commands.

In the article, find the section titled "Java Compilation Process". Focus on the descriptions for the commands and how they correspond to the compile and execute phases. Notice how the java command is used on the class name without the .class extension.


The Professional Workflow: Using an IDE

While the command line is essential for understanding the fundamentals, modern software development relies on Integrated Development Environments (IDEs). An IDE like IntelliJ IDEA bundles a text editor, the compiler, the runtime, a debugger, and many other tools into a single, cohesive application.

For the interns, the transition to an IDE demonstrates the importance of professional tooling for productivity. The IDE automates the compile-and-run steps, provides intelligent code completion, and helps catch errors before compilation.

The process in an IDE generally involves:

  1. Creating a "project" to house all the source code and related files.
  2. Creating a Java class file within the project structure.
  3. Writing the code, assisted by the IDE's features.
  4. Clicking a "Run" button, which triggers the IDE to compile and execute the code, displaying the output in a built-in console window.

This video from JetBrains provides a comprehensive tour of creating and running your first application in IntelliJ IDEA.

Creating your first Java application with IntelliJ IDEA

This official tutorial shows how to get started with a Java project in IntelliJ IDEA. It covers project creation, coding, and execution within the IDE.

Watch the following segments to understand the standard workflow: Creating a new project, where you can select the project name and the JDK version. Creating a new Java class within a package structure. This is the standard approach your interns should use. Writing and running the code, showcasing how the IDE helps generate the main method and how to run the application with a single click. Using the integrated terminal, which shows that you can still access the command line directly from the IDE if needed.

For those who prefer a text-based guide, the following article from Oracle's dev.java provides a written walkthrough of the same process.

Building a Java application in IntelliJ IDEA - Dev.java

This article provides step-by-step instructions for creating, coding, and running a Java application in IntelliJ IDEA.

Read through the following sections: Creating a new project: This walks through the setup wizard. Writing and editing code: This explains how to create a class and add the main method. For your course, focus on the instructions for a regular Java class, not the "compact source file" which is a newer, less common feature. Running your application: This shows how to execute your code using the IDE's run buttons.


Conclusion

You have now covered the two primary ways a Java program is brought from source code to execution. This practical knowledge complements the theory of the Java platform and equips your interns with the skills to both understand the fundamentals and work efficiently.

Here are the key takeaways from this lesson:

  • Command-Line Workflow: A two-step process using javac (the compiler from the JDK) to create a .class file, and java (the launcher from the JRE) to execute the bytecode. This method reveals the underlying mechanics of the Java platform.
  • IDE Workflow: A streamlined process where creating, writing, compiling, and running code are integrated into a single environment. This is the standard for professional development, boosting productivity and reducing manual steps.
  • The Entry Point: The public static void main(String[] args) method is the mandatory starting point for any standalone Java application.

With this foundation, your interns are now ready to move beyond simply printing a static message. In the next lesson, we will begin building the core of the Smart Warehouse System by learning how to declare variables using Java's primitive and reference types to represent essential warehouse data like product IDs, quantities, and prices.