Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

javac: error: package does not exist

The compiler cannot see the jar. Put it on the classpath, or let Maven or Gradle manage scopes so compile time and runtime agree on what exists.

~5 min readJava
#java#javac#classpath#build-tools
Bold white headline reading PACKAGE MISSING with a Java compiler crate holding an amber jar slot empty beside a cyan classpath rail.

Introduction & Quick Summary

A single Java file that compiles in the IDE fails from the terminal:

code
Main.java:3: error: package org.apache.commons.lang3 does not exist import org.apache.commons.lang3.StringUtils; ^ 1 error

Quick answer: the compiler cannot see the jar. Put it on the classpath.

bash
javac -cp "lib/*" -d out $(find src -name '*.java') java -cp "out:lib/*" Main

package does not exist is an import error, and it is reported before symbol resolution. javac read import org.apache.commons.lang3.StringUtils;, looked for a package named org.apache.commons.lang3 on the compile classpath and the source path, and found nothing. The ^ sits under the imported type name because that is the token whose resolution failed.

Keeping this distinct from cannot find symbol saves time. package does not exist means the dependency is absent or invisible. cannot find symbol means the package was found but the class, method or variable inside it was not — a typo, a wrong signature or a missing import statement in an otherwise present dependency.

Core Mechanics, Signatures & Reproducible Setup

Reproduce it without any build tool:

bash
mkdir -p demo/src/com/example && cd demo cat > src/com/example/App.java <<'JAVA' package com.example; import org.apache.commons.lang3.StringUtils; public class App { public static void main(String[] args) { System.out.println(StringUtils.capitalize("hello")); } } JAVA javac -d out src/com/example/App.java # error: package org.apache.commons.lang3 does not exist

Now download the jar, put it in lib/ and compile again:

curl -sLO https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.17.0/commons-lang3-3.17.0.jar mkdir -p lib && mv commons-lang3-3.17.0.jar lib/ javac -cp "lib/*" -d out src/com/example/App.java java -cp "out:lib/*" com.example.App

The classpath is a design decision, not a detail. Every entry is a directory containing package folders or a jar file; lib/* expands to every jar in lib, and lib alone would make javac look for a file named lib/org/apache/.... Entry order matters when two jars contain the same class: the first match wins, silently.

Situationjavac's answerWhat it means
jar not on the classpathpackage X does not existadd it with -cp
jar on the classpath, class renamedcannot find symbol at the usagewrong artifact version
wrong package line in the filepackage X does not exist for a sibling importfix the declared package
dependency marked providedcompiles, fails at runtimeNoClassDefFoundError
dependency only in test scopecompiles in tests, not in mainmove the scope

Key Comparison Table

Classpath syntax differs by shell and OS, and a wrong separator is the second most common cause after a missing jar:

FormSeparatorExpands jarsWhere it applies
-cp lib/a.jar:lib/b.jar:noLinux, macOS
-cp "lib/a.jar;lib/b.jar";noWindows
-cp "lib/*"noneyesevery platform, inside quotes
CLASSPATH=lib/* envplatformyesinherited by javac and java
-cp out:lib/*platformyesrunning a built program
manifest Class-Pathspacesnoexecutable jars

The build tools express the same thing, and each has a scope that decides whether a dependency reaches the compiler:

Tool and scopeOn compile classpathOn runtime classpathTypical use
Maven compileyesyeslibraries used in main code
Maven providedyesnoservlet API, JDK-provided APIs
Maven runtimenoyesJDBC drivers
Maven testtests onlytests onlyJUnit, Mockito
Gradle implementationyesyesdefault choice
Gradle compileOnlyyesnoannotations, provided APIs

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. Maven projects: ask the build, not the terminal

bash
mvn -q dependency:tree -Dincludes=org.apache.commons mvn -q dependency:tree -Dscope=compile | grep commons mvn -q -X compile | grep -i classpath | head -3

If the dependency does not appear in the tree, the pom.xml is wrong. If it appears with scope provided or test, the compiler sees it but the runtime will not. mvn dependency:tree is faster than reading a 300-line pom, and filtering its output with the regex tester turns a long tree into the two lines that matter. A dependency that arrives transitively and disappears after an upgrade usually means someone added an <exclusions> block upstream; declaring it explicitly at the version you need is the stable fix.

2. Gradle projects: implementation versus api

kotlin
dependencies { implementation("org.apache.commons:commons-lang3:3.17.0") compileOnly("org.projectlombok:lombok:1.18.34") annotationProcessor("org.projectlombok:lombok:1.18.34") }

In a multi-module build, implementation is not exported to consumer modules; code in module B that imports a class from module A's implementation dependency fails with package does not exist even though A compiles. Either the class should not be part of A's public API, or the dependency belongs in api. Lombok needs both lines: compileOnly for the annotations and annotationProcessor to generate code. Missing the processor produces cannot find symbol for generated getters, which looks unrelated.

3. javac directly: quoting, wildcards and stale output

bash
# wrong: the shell expands lib/* into separate arguments javac -cp lib/* -d out src/App.java # right: quoted, so javac expands it javac -cp "lib/*" -d out src/App.java # compiling a tree find src -name '*.java' > sources.txt javac -cp "lib/*" -d out @sources.txt

@sources.txt avoids the "argument list too long" limit on large trees. Also delete out/ when the classpath changed: javac reuses class files it finds on the classpath, and a stale out directory can hide a missing dependency until a clean build in CI.

4. Module path and module-info.java

A modular project that puts its dependencies on the classpath instead of the module path gets import errors that look like missing jars:

bash
javac --module-path "lib" --add-modules org.apache.commons.lang3 -d out \ src/main/java/module-info.java src/main/java/com/example/App.java
java
module com.example.app { requires org.apache.commons.lang3; exports com.example.app; }

Three related messages follow from the same setup: package org.apache.commons.lang3 is not visible (module not required), module not found: org.apache.commons.lang3 (jar not on the module path) and package does not exist (jar on neither path). A plain jar without module-info.class becomes an automatic module named after its file name, so commons-lang3-3.17.0.jar is org.apache.commons.lang3 only because the jar declares Automatic-Module-Name; otherwise the name would be derived from the file name with dashes turned into dots.

5. javax to jakarta, and split packages

The Jakarta EE rename moves every package: javax.servlet.http.HttpServlet became jakarta.servlet.http.HttpServlet. A project that upgrades the API jar but keeps old imports gets package javax.servlet.http does not exist, and the fix is a coordinated change of imports, the server version and the dependency version. Split packages — two jars contributing to the same package — are legal on the classpath and illegal on the module path, so a library set that compiles today can fail the moment module-info.java is added.

6. IDE works, CLI fails (and the reverse)

The IDE builds its own classpath from the project model, so the two environments disagree whenever the model and the files do. Check which:

bash
# what classpath does the build tool actually use? mvn -q help:evaluate -Dexpression=project.build.outputDirectory -DforceStdout ls target/classes target/dependency 2>/dev/null | head

The common cases are an IDE-only library reference pointing at an absolute path on one machine, a source folder not registered as a source root, and an annotation processor configured only in the IDE. Verify a downloaded jar before trusting it — sha256sum it and compare with the checksum from Maven Central using the hash tool; a truncated jar produces exactly this error.

Common Compilation/Runtime Mistakes & Version Gotchas

Setting -cp and expecting CLASSPATH to still apply. The flag replaces the environment variable rather than extending it. Repeat every entry in the flag.

Compiling with one version and running with another. --release 17 compiles against the 17 API; running with an older JRE gives UnsupportedClassVersionError, and an older jar on the runtime classpath gives NoClassDefFoundError for a method that exists only in the newer version.

Assuming a transitive dependency is permanent. Dependencies move between artifacts; declare what you import.

Adding the jar to the IDE only. A CI build with a fresh checkout uses the pom or the build file, never the IDE metadata.

Version notes. javac has printed the package does not exist wording unchanged for two decades, but the mapping changed with JDK 9 modules: on the module path the compiler reports module not found instead. --release replaced -source/-target for API correctness from JDK 9. JDK 11 removed the EE modules from the JDK itself, which is why old code importing javax.xml.bind now fails to compile without an explicit dependency.

A durable habit. Compile with the same command CI uses, from a clean target/ or build/ directory, before pushing. The error above never survives that check.

Reference material: javac and Maven dependency mechanism.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.