Running Java Programs from the Command Line on Windows

Introduction

This tutorial provides a step-by-step guide to running Java programs from the command line on a Windows operating system. We will cover compiling and executing Java code, creating executable JAR files, handling package structures, and using single-file execution available in recent Java versions.

Prerequisites

Before you begin, ensure that:

  • You have Java Development Kit (JDK) installed on your machine.
  • Your JAVA_HOME environment variable is set to the JDK installation directory.
  • The bin directory of your JDK is added to your system’s PATH variable.

Compiling and Running a Java Program

Step 1: Navigate to Your Source Directory

Open Command Prompt and use the cd command to navigate to the directory containing your .java file. For example:

C:\> cd C:\mywork

Step 2: Compile Your Java File

Compile your Java source code into bytecode (.class files) using the javac compiler.

C:\mywork> javac CopyFile.java

If there are no syntax errors, you should see a new file named CopyFile.class.

Step 3: Run Your Java Program

Execute your program with the java command. Note that you should not include the .class extension:

C:\mywork> java CopyFile

This will display any output from your program, such as "File is copied successful!".

Creating and Running a JAR File

JAR (Java ARchive) files bundle Java classes and resources into a single file, which can be easily distributed and executed.

Step 1: Compile Your Class Files

First, compile all the necessary .java files in your project directory.

C:\mywork> javac *.java

Step 2: Create the JAR File

Method 1: Using a Manifest File

Create a MANIFEST.MF file specifying the main class:

Main-Class: CopyFile

Then, package your classes into a JAR file with:

C:\mywork> jar -cvfm MyProgram.jar MANIFEST.MF *.class

Method 2: Using the Entry Point Option

Alternatively, specify the main class directly in the jar command:

C:\mywork> jar -cvfe MyProgram.jar CopyFile *.class

Step 3: Run Your JAR File

Execute your packaged application with:

java -jar MyProgram.jar

Handling Packages

When working with Java packages, the directory structure must reflect the package hierarchy. Suppose you have a class CopyFile.java in the package com.example.utility.

Compile and Run with Package Structure

  1. Compile:
    Navigate to your source directory (the root of your package hierarchy) and compile using:

    C:\SimpleJavaProject\src> javac com/example/utility/CopyFile.java
    
  2. Run:
    Execute the program by specifying the full package name followed by the class name:

    C:\SimpleJavaProject\src> java com.example.utility.CopyFile
    

Single-File Execution in Java 11 and Later

For simplicity, starting with Java 11, you can run a single-file source directly using the java command without explicitly compiling it first:

C:\mywork> java CopyFile.java

This feature allows for quick prototyping but note that it applies only to programs contained within a single file.

Conclusion

By following these steps, you should be able to compile and execute Java programs from the command line on Windows. Remember to adhere to directory structures when dealing with packages and take advantage of JAR files for distributing your applications. For advanced features like single-file execution, ensure that you are using a compatible version of Java.

Leave a Reply

Your email address will not be published. Required fields are marked *