How to compile and run a Java application from the command line

JavaJavaBeginner
Practice Now

Introduction

Java is a powerful programming language used in a wide range of applications, from enterprise software to mobile apps. In this tutorial, we'll explore the process of compiling and running Java applications from the command line, a crucial skill for any Java developer.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/BasicSyntaxGroup -.-> java/output("`Output`") subgraph Lab Skills java/user_input -.-> lab-413959{{"`How to compile and run a Java application from the command line`"}} java/identifier -.-> lab-413959{{"`How to compile and run a Java application from the command line`"}} java/output -.-> lab-413959{{"`How to compile and run a Java application from the command line`"}} end

Introduction to Java Programming

Java is a popular and widely-used programming language that was first released by Sun Microsystems in 1995. 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) philosophy, which means that Java programs can be compiled into a platform-independent bytecode that can be executed on any Java Virtual Machine (JVM). This makes Java a highly portable and versatile language, as developers can write code once and deploy it on multiple platforms without having to rewrite or recompile the code.

Java is used in a wide range of applications, from desktop applications and web applications to mobile apps and enterprise-level software. It is particularly popular in the enterprise software development community, where it is used for building large-scale, mission-critical applications.

To get started with Java programming, you'll need to have the Java Development Kit (JDK) installed on your system. The JDK includes the Java compiler, which is used to convert Java source code into bytecode, as well as the Java Runtime Environment (JRE), which is used to execute Java programs.

Once you have the JDK installed, you can start writing and compiling Java code using a text editor or an Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA.

graph TD A[Java Development Kit (JDK)] --> B[Java Compiler] A --> C[Java Runtime Environment (JRE)] B --> D[Java Bytecode] D --> C[Java Runtime Environment (JRE)] C --> E[Java Application]

In the next section, we'll explore how to compile Java source code using the command line.

Compiling Java Source Code

To compile Java source code, you can use the Java compiler, which is included in the Java Development Kit (JDK). The Java compiler is a command-line tool that converts Java source code into bytecode, which can then be executed by the Java Runtime Environment (JRE).

Compiling a Single Java File

To compile a single Java file, you can use the javac command followed by the name of the Java file. For example, to compile a file named HelloWorld.java, you would run the following command in the terminal:

javac HelloWorld.java

This will create a compiled class file named HelloWorld.class in the same directory as the source file.

Compiling Multiple Java Files

If your Java application consists of multiple source files, you can compile them all at once by listing the file names after the javac command. For example:

javac HelloWorld.java AnotherClass.java MyClass.java

This will compile all three Java files and create the corresponding class files.

Compiling Java Files in Subdirectories

If your Java source files are organized in a directory structure, you can still compile them using the javac command. For example, if you have a file named com/example/MyClass.java in a subdirectory, you can compile it with the following command:

javac com/example/MyClass.java

This will create the MyClass.class file in the com/example directory.

graph TD A[Java Source File] --> B[Java Compiler (javac)] B --> C[Java Bytecode (.class)]

In the next section, we'll explore how to run Java applications from the command line.

Running Java Applications

After compiling your Java source code into bytecode, you can run the Java application using the java command. The java command is used to launch the Java Virtual Machine (JVM) and execute the compiled Java bytecode.

Running a Single Java Class

To run a single Java class, you can use the java command followed by the fully qualified class name (including the package name if applicable). For example, to run a class named HelloWorld in the default package, you would use the following command:

java HelloWorld

If the HelloWorld class is in a package named com.example, you would use the following command:

java com.example.HelloWorld

Running a Java Application with Command-Line Arguments

You can also pass command-line arguments to your Java application by including them after the class name. For example:

java HelloWorld John Doe

Inside your Java code, you can access these command-line arguments using the args parameter of the main() method.

public static void main(String[] args) {
    System.out.println("Hello, " + args[0] + " " + args[1] + "!");
}

Running Java Applications from JAR Files

If your Java application is packaged into a JAR (Java Archive) file, you can run it using the java command and specifying the JAR file. For example:

java -jar myapp.jar

This will launch the JVM and execute the main class specified in the JAR file's manifest.

graph TD A[Java Bytecode (.class)] --> B[Java Virtual Machine (JVM)] B --> C[Java Application]

By following these steps, you can successfully compile and run Java applications from the command line, regardless of the complexity of your project or the way your source files are organized.

Summary

By the end of this tutorial, you'll have a solid understanding of how to compile and run Java applications from the command line. This knowledge will empower you to develop, test, and deploy your Java programs with confidence, laying the foundation for your journey as a Java programmer.

Other Java Tutorials you may like