JARs in classpath: How to include all libraries from a folder in 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: The Curse of a Thousand JARs

\n

Have you ever found yourself in a situation where your Java project grows, and the lib/ folder turns into a real zoo of dozens, or even hundreds, of JAR files? And every time you launch the application, you painfully try to remember which JAR you added and which one you forgot? Sounds familiar, doesn't it?

\n

The question "Including all the jars in a directory within the Java classpath" is one of the most popular on Stack Overflow. And for good reason. A seemingly trivial task becomes a headache if you don't know the right tools. In this article, we will cover all the methods: from ancient ones, as old as dinosaurs, to the most modern ones that every self-respecting Java developer uses in 2025.

\n

Get ready, we are embarking on an exciting journey through the jungles of the classpath!

\n\n

Why is this even a problem? (or "The -cp Epic")

\n

In the good old days (before Java 6), developers were forced to list each JAR file manually. It looked something like this:

\n
java -cp lib/guava.jar:lib/spring-core.jar:lib/log4j.jar:lib/jackson.jar MyApp
\n

Now imagine you have 30 libraries. One typo — and the application crashes with ClassNotFoundException. A nightmare! That's when the community started looking for ways to include all jars in classpath Java with a single line.

\n\n

Method #1: Wildcard — The Savior from Java 6

\n

Starting with Java 6, a magical feature appeared in the JVM — the wildcard. You simply specify the path to the folder with an asterisk, and the JVM itself loads all JAR files from there.

\n

How it works:

\n
java -cp "lib/*" MyApp
\n

Important! Pay attention to the quotes. Without them, in some shells (e.g., on Unix), the asterisk may be interpreted by the shell, and you will get something unexpected. Quotes ensure that the * is passed directly to the JVM.

\n

This method is still relevant today. It is simple, elegant, and solves 90% of the problems. But there is a nuance: the wildcard does not work recursively. If you have nested folders (lib/subdir/), their contents will not be loaded. For this, you need to specify each level manually:

\n
java -cp "lib/*:lib/subdir/*" MyApp
\n\n

When to use wildcard?

\n
    \n
  • You are writing a simple script or utility.
  • \n
  • You don't have a build system (Maven/Gradle).
  • \n
  • You need to quickly run something "on the fly".
  • \n
\n\n

Method #2: Maven — The Industry Standard

\n

If you are working on a real project, you are most likely using Maven. And the question "how to include all JARs from a folder" is solved at the build level.

\n

Maven manages dependencies itself. You simply specify them in pom.xml, and Maven downloads the required JARs and adds them to the classpath automatically.

\n

Example pom.xml:

\n
<dependencies>\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-web</artifactId>\n        <version>3.2.0</version>\n    </dependency>\n    <dependency>\n        <groupId>com.google.code.gson</groupId>\n        <artifactId>gson</artifactId>\n        <version>2.10.1</version>\n    </dependency>\n</dependencies>
\n

During the build (e.g., mvn package), Maven creates a fat JAR (or uses the maven-dependency-plugin), which includes all dependencies. You no longer need to think about the classpath — it is packaged inside.

\n

But what if you need to include all JARs from some local folder not managed by Maven? For this, there are System Dependencies (although this is considered an anti-pattern):

\n
<dependency>\n    <groupId>com.example&                

Blogs

Book Recommendations