Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Add a JAR File to the Build Path Using Windows Command Line

How to Add a JAR File to the Build Path Using Windows Command Line

Managing Java projects often requires adding external libraries, such as JAR files, to your build path. While integrated development environments (IDEs) like Eclipse or IntelliJ IDEA provide graphical interfaces for this task, sometimes you need to do it via the Windows command line—especially for automated builds, scripting, or working in environments without GUI access. This guide walks you through the process of adding a JAR file to your Java project’s build path using Windows command-line tools, ensuring your project recognizes the external libraries needed for compilation and execution.

What You’ll Need

  • A Windows computer with command prompt access
  • Java Development Kit (JDK) installed and configured in your system PATH
  • The JAR file you want to add, with its full file path
  • Your Java project directory or source files
  • Basic familiarity with command-line navigation and commands

Step-by-Step

  1. Open Command Prompt: Press Win + R, type cmd, and hit Enter to open the Windows command line interface.
  2. Navigate to your project directory: Use the cd command to change to the folder containing your Java source files or build scripts. For example:
    cd C:\Users\YourName\Projects\MyJavaProject
  3. Identify the full path of your JAR file: Ensure you know the complete file path, e.g., C:\Libraries\external-lib.jar.
  4. Compile your Java source files with the JAR added to the classpath: Use the javac command with the -cp or --class-path option. For example:
    javac -cp .;C:\Libraries\external-lib.jar *.java

    This command compiles all Java files in the current directory, including the external JAR in the build path.

  5. Run your Java application with the JAR included: Use the java command with the -cp option, specifying both your project directory and JAR file. For example:
    java -cp .;C:\Libraries\external-lib.jar com.mycompany.MyMainClass

    Replace com.mycompany.MyMainClass with your actual main class’s fully qualified name.

  6. Optionally, create a batch script for repeated builds: Save your compile and run commands into a .bat file for easier reuse. For example:
    @echo off
    javac -cp .;C:\Libraries\external-lib.jar *.java
    java -cp .;C:\Libraries\external-lib.jar com.mycompany.MyMainClass

Common Mistakes

Scroll to top