How to compile and run a Java program in the terminal

JavaJavaBeginner
Practice Now

Introduction

Java is a powerful and versatile programming language used across a wide range of applications. In this tutorial, we will guide you through the process of compiling and running Java programs directly in the terminal or command line interface. This is a fundamental skill for any Java developer, allowing you to quickly test and execute your code without the need for an integrated development environment (IDE).


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/user_input -.-> lab-413960{{"`How to compile and run a Java program in the terminal`"}} java/output -.-> lab-413960{{"`How to compile and run a Java program in the terminal`"}} java/system_methods -.-> lab-413960{{"`How to compile and run a Java program in the terminal`"}} end

Introduction to Java Programming

Java is a popular and widely-used programming language that was first released in 1995 by Sun Microsystems. It is an object-oriented, class-based language that is designed to be platform-independent, meaning that Java programs can run on a variety of operating systems, including Windows, macOS, and Linux.

One of the key features of Java is its "write once, run anywhere" (WORA) principle, which means that Java programs can be compiled once and then executed on any Java Virtual Machine (JVM) without the need for recompilation. This makes Java a versatile and portable language that is widely used in a variety of applications, including web development, mobile app development, enterprise software, and scientific computing.

Java programs are typically written in a text editor or an Integrated Development Environment (IDE) such as IntelliJ IDEA, Eclipse, or NetBeans. Once the source code is written, it must be compiled into bytecode, which is a platform-independent representation of the program that can be executed by the JVM.

To get started with Java programming, you will need to install the Java Development Kit (JDK) on your system. The JDK includes the Java compiler, the JVM, and various other tools and libraries that are necessary for developing and running Java programs.

graph TD A[Write Java Code] --> B[Compile Java Code] B --> C[Run Java Program] C --> D[Execute on JVM]

Once you have the JDK installed, you can start writing and compiling Java programs using the command line or an IDE. In the next section, we will explore how to compile Java source code using the command line.

Compiling Java Source Code

To compile Java source code, you can use the javac command, which is part of the Java Development Kit (JDK). The javac command takes a Java source file as input and generates a corresponding bytecode file with a .class extension.

Here's an example of how to compile a simple Java program:

  1. Create a new file called HelloWorld.java with the following content:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
  1. Open a terminal and navigate to the directory where you saved the HelloWorld.java file.

  2. Compile the Java source code using the javac command:

javac HelloWorld.java

After running this command, you should see a new file called HelloWorld.class in the same directory. This file contains the bytecode representation of your Java program.

The compilation process can be summarized as follows:

graph TD A[Java Source Code] --> B[javac Compiler] B --> C[Java Bytecode]

It's important to note that the javac command can also be used to compile multiple Java source files at once. For example, if you have a directory with several Java files, you can run javac *.java to compile all of them.

Additionally, the javac command supports various options and flags that can be used to customize the compilation process, such as setting the source and target Java versions, enabling or disabling certain compiler warnings, and more. You can explore these options by running javac -help in the terminal.

Now that you've learned how to compile Java source code, let's move on to the next step: running Java programs in the terminal.

Running Java Programs in the Terminal

After compiling your Java source code, you can run the resulting bytecode using the java command. The java command is also part of the Java Development Kit (JDK) and is used to execute Java programs.

To run the HelloWorld program we compiled earlier, follow these steps:

  1. Open a terminal and navigate to the directory where the HelloWorld.class file is located.

  2. Run the java command, followed by the name of the main class (without the .class extension):

java HelloWorld

This will execute the main() method in the HelloWorld class and output the message "Hello, World!" to the console.

The process of running a Java program can be summarized as follows:

graph TD A[Java Bytecode] --> B[java Command] B --> C[Execute on JVM]

In addition to running simple programs, the java command also supports various options and flags that can be used to customize the execution of your Java application. For example, you can use the -cp or -classpath option to specify the classpath, which is the directory or set of directories where the JVM should look for the compiled class files.

Here's an example of running a Java program with a custom classpath:

java -cp /path/to/my/classes HelloWorld

You can also pass command-line arguments to your Java program using the String[] args parameter in the main() method. These arguments can be accessed and used within your Java code.

public class HelloWorld {
    public static void main(String[] args) {
        if (args.length > 0) {
            System.out.println("Hello, " + args[0] + "!");
        } else {
            System.out.println("Hello, World!");
        }
    }
}

To run this program with a command-line argument, you would use the following command:

java HelloWorld "LabEx"

This would output "Hello, LabEx!" to the console.

By understanding how to compile and run Java programs in the terminal, you have taken the first steps towards becoming a proficient Java developer. In the next section, we'll explore more advanced topics and techniques for working with Java.

Summary

By following the steps outlined in this tutorial, you will learn how to compile Java source code and run Java programs directly in the terminal. This knowledge will empower you to work efficiently with Java, whether you're a beginner or an experienced developer. Understanding the terminal-based workflow for Java development is a valuable skill that will serve you well throughout your programming journey.

Other Java Tutorials you may like