How to Execute a Java Program from the Command Line in Windows

Question

What steps should I follow to run a Java program from the command line on Windows?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyFile {
    public static void main(String[] args) {
        InputStream inStream = null;
        OutputStream outStream = null;
        
        try {
            File afile = new File("input.txt");
            File bfile = new File("inputCopy.txt");
            
            inStream = new FileInputStream(afile);
            outStream = new FileOutputStream(bfile);
            
            byte[] buffer = new byte[1024];
            
            int length;
            
            // copy the file content in bytes
            while ((length = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
            }
            
            inStream.close();
            outStream.close();
            
            System.out.println("File is copied successful!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Answer

To run a Java program from the command line on Windows, you need to ensure that you have Java installed and set up correctly. You'll use the Command Prompt to compile and execute your Java program, following these steps:

// To compile and run your Java application:
// Step 1: Open Command Prompt and navigate to the directory containing CopyFile.java
// Step 2: Compile the program:
javac CopyFile.java
// Step 3: Run the compiled class:
java CopyFile
// Ensure input.txt is in the same directory or provide the full path.

Causes

  • Java Development Kit (JDK) is not installed or configured correctly.
  • Environment variables are not set, particularly the PATH variable.
  • The program file is not in the correct directory when you attempt to run it.

Solutions

  • Install the latest version of the Java Development Kit (JDK) from the official Oracle or OpenJDK website.
  • Ensure the PATH environment variable includes the path to the 'bin' directory of your JDK installation (for example: C:\Program Files\Java\jdk-<version>\bin).
  • Open the Command Prompt and navigate to the directory where your Java file is located using the 'cd' command.

Common Mistakes

Mistake: Not adding Java to the system PATH variable.

Solution: Go to "Control Panel > System and Security > System > Advanced system settings > Environment Variables" and add the JDK bin directory to the PATH.

Mistake: Compiling from a different directory than where the Java file is located.

Solution: Use the 'cd' command in the Command Prompt to change to the directory of the Java file.

Mistake: File not found error when running the program.

Solution: Ensure the input.txt file is present in the same directory or specify the complete path in the Java code.

Helpers

  • Run Java program Windows
  • Execute Java from command line
  • Java command line compilation
  • Java JDK installation Windows
  • Run Java application command prompt

Related Questions

⦿Comparing System.currentTimeMillis(), new Date(), and Calendar.getInstance().getTime() in Java

Explore the performance and resource implications of System.currentTimeMillis new Date and Calendar.getInstance.getTime in Java applications.

⦿What Are the Differences Between junit.framework.Assert and org.junit.Assert Classes in JUnit?

Explore the differences between junit.framework.Assert and org.junit.Assert classes in JUnit their usage and how they impact your testing strategy.

⦿How to Find and View TODO Tags in Eclipse IDE

Learn how to easily locate TODO comments in Eclipse including autogenerated methods and custom TODO tags with stepbystep instructions.

⦿Why Should You Prefer Java's ArrayDeque Over LinkedList?

Discover why ArrayDeque is a superior choice compared to LinkedList in Java including implementation details and performance benefits.

⦿How to Pad a String with Leading Zeros in Java

Learn how to format a Java String with leading zeros to achieve a specific length using various methods. Perfect for developers seeking string manipulation techniques.

⦿Understanding the Differences Between Boolean and boolean in Java

Explore the key differences between Boolean and boolean in Java including default values and best practices for usage.

⦿How to Set Java Compiler Version in a Maven pom.xml File?

Learn how to specify the Java compiler version in your Maven pom.xml file to avoid compatibility issues.

⦿Why Does getResourceAsStream Return Null in Java When Loading Resources from a JAR?

Learn why getResourceAsStream returns null in Java and how to properly load resources from a JAR file with examples and solutions.

⦿Understanding Java Memory Pool Distribution: Heap and Non-Heap Memory Differences

Learn about Java Memory Pools including Heap NonHeap and their components like Eden Space Survivor Space and more.

⦿What is the Difference Between int[] array and int array[] in Java?

Explore the differences between int array and int array in Java. Learn how array syntax works and discover best practices.

© Copyright 2025 - CodingTechRoom.com