To include external jars you can add the dependency with System scope.

<dependency>
   ..
   <scope>system<scope>
   <systemPath>your jar path</systemPath>
</dependency>

Also you can define your compiler plugin to include the directory in your classpath.

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>directory path/*.jar</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>

NOTE: This will make your build success. But not sure how to make those classes available for access inside IDE.

Answer from Rima on Stack Overflow
🌐
Blogger
javarevisited.blogspot.com › 2016 › 07 › eclipse-how-to-add-external-jar-into-classpath.html
Eclipse - How to add/remove external JAR into Java Project's Classpath? Example
It will open a selection window to choose the JAR file from the existing Eclipse project and add that into your application's classpath. 7) If the automatic build is not set then make sure you refresh your project and build it again. That's all about how to add/remove JAR files into your project's classpath in Eclipse. If you are using Maven with Eclipse e.g.
Discussions

java - How to put a jar in classpath in Eclipse? - Stack Overflow
Hi I am n00b in classpath and Ant. While reading the tutorial of GCM for Android I came across a line Step 1: Copy the gcm.jar file into your application classpath To write your Android applicat... More on stackoverflow.com
🌐 stackoverflow.com
How do I add jar files to classpath? Specifically, I am using Eclipse and I want to add Quartz scheduler jar files
Might depend on your system, but you should be able to do the following In Eclipse: Right click your project, select Build Path > Configure Build Path... Tab over to Libraries Add external JARs... > Find the JAR you're looking for Should be good to go. You might require a refresh or something similar More on reddit.com
🌐 r/java
10
3
February 3, 2014
maven - how to add my external jar file to class path - Stack Overflow
Thanks for your answer, I wrote simple application in eclipse by importing and come to know the jars dependencies and added those dependencies to pom.xml, it solved my problem. ... That way it'll create a single-jar of large size and build time will be large everytime you try to build. I instead prefer adding all jars to a lib folder and including in the classpath ... More on stackoverflow.com
🌐 stackoverflow.com
Maven: add a folder or jar file into current classpath - Stack Overflow
I am using maven-compile plugin to compile classes. Now I would like to add one jar file into the current classpath. That file stays in another location (let's say c:/jars/abc.jar . I prefer to leave More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › java › jvm › ways to add jars to classpath in java
Ways to Add JARs to Classpath in Java | Baeldung
July 19, 2025 - We can easily add JAR files to our project’s classpath using popular IDEs like Eclipse or IntelliJ. Both IDEs provide user-friendly interfaces to include external libraries in our project. For detailed instructions, we can refer to the IDE-specific documentation for the most accurate and up-to-date guidance. While the above methods work well for small projects, projects both large and small can take advantage of a build tool. Maven and Gradle are the popular build tools used for this purpose.
Top answer
1 of 2
5

Maven out of the box will come up with a JAR file (default packaging). This JAR file only contains (main) artifacts of the project. If you take just that and run it, clearly the dependencies are missing -- by design.

Typically Maven artifacts are reused in combination with their POM so that at the point of use it's know what the dependencies are. Edit: if you're using APKs and installing them on a phone, there may be mechanisms to deal with dependencies, I'm answering this merely from a Maven standpoint.

If you want to create a JAR with dependencies you have to tell Maven to do so, that's not the default. Ways of having Maven do that are (probably not exhaustive):

  • Maven Assembly plugin, jar-with-dependencies predefined descriptor:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
    ...
    
  • Maven Shade plugin

2 of 2
3

That way it'll create a single-jar of large size and build time will be large everytime you try to build.

I instead prefer adding all jars to a lib folder and including in the classpath (jar's manifest), because of which when we have to make some change or redeploy to the client or some place, we can simply give the small jar (not all the dependencies merged within jar)

          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.kalindiinfotech.webcrawler.MainGUI</mainClass>
                            <!--                            <mainClass>com.KalindiInfotech.busbookingmaven.form.LoginForm</mainClass>-->
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>install</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
🌐
javaspring
javaspring.net › blog › eclipse-adding-all-the-jars-from-a-folder-in-java-classpath
Eclipse: How to Add All JAR Files from a Folder to Java Classpath [Step-by-Step Guide] — javaspring.net
Copy/paste all your JAR files into this lib folder (drag-and-drop from your file explorer to Eclipse, or use File > Import > File System). Why? Storing JARs in lib ensures the project references relative paths, so teammates won’t need to update ...
Find elsewhere
🌐
Java67
java67.com › 2017 › 04 › how-to-add-jar-file-in-eclipse-project.html
How to add JAR files in Eclipse Project's Build path? Example | Java67
In this Java Eclipse tutorial, I will show you two ways to add external JAR files in Eclipse Java projects. Many times we need to use external JAR files in our Java application for different needs like for general purposes you may use Google Guava or Apache Commons. If you are using Spring or Hibernate framework then you need their JAR files as well. In order to use any third-party library or framework, we have to add their JAR files in the classpath, to compile and run our Java programs.
🌐
Eclipse
archive.eclipse.org › eclipse › downloads › documentation › 2.0 › html › plugins › org.eclipse.jdt.doc.user › tasks › tasks-114.htm
Adding a classpath variable to the build path
Select the project to which you want to add the classpath variable ... In the Properties page, select the Java Build Path page. On the Libraries tab, click Add Variable for adding a variable that refers to a JAR file. The New Variable Classpath Entry dialog appears which shows all available ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Include Jars In Java Classpath Example - Java Code Geeks
February 13, 2025 - Once added, Eclipse automatically includes the JAR in the classpath for compilation and execution. ... Go to File -> Project Structure -> Modules. Click on the Dependencies tab and then click the + (Add) button.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-add-jar-file-to-classpath-in-java
How to Add JAR file to Classpath in Java? - GeeksforGeeks
August 7, 2021 - It can be either Classpath or classpath which is similar to PATH environment variable which we can use to locate Java binaries like javaw and java command. Command 2: By including name of JAR file in -a classpath command-line option
🌐
Wikihow
wikihow.com › computers and electronics › software › programming › java › how to add jars to project build paths in eclipse (java)
How to Add JARs to Project Build Paths in Eclipse (Java)
August 10, 2022 - They can define it under Window->Preferences->Java->Build Path->Classpath Variables. ... Right-click the project name. This displays a pop-up menu to the side. Note: If you use this method, the external JAR will need to be in the same location ...
🌐
Mkyong
mkyong.com › home › maven › how to add m2_repo classpath variable to eclipse ide
How to add M2_REPO classpath variable to Eclipse IDE - Mkyong.com
December 5, 2011 - Define and add M2_REPO classpath variable manually into Eclipse IDE. Follow below steps : ... Click on the new button > defined a new M2_REPO variable and point it to your local Maven repository
Top answer
1 of 5
12

This might have been asked before. See Can I add jars to maven 2 build classpath without installing them?

In a nutshell: include your jar as dependency with system scope. This requires specifying the absolute path to the jar.

See also http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

2 of 5
4

The classpath setting of the compiler plugin are two args. Changed it like this and it worked for me:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.1</version>
    <configuration>
      <compilerArgs>
         <arg>-cp</arg>
         <arg>${cp}:${basedir}/lib/bad.jar</arg>
      </compilerArgs>
    </configuration>
   </plugin>

I used the gmavenplus-plugin to read the path and create the property 'cp':

<plugin>
        <!--
          Use Groovy to read classpath and store into
          file named value of property <cpfile>

          In second step use Groovy to read the contents of
          the file into a new property named <cp>

          In the compiler plugin this is used to create a
          valid classpath
        -->
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.12.0</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <!-- any version of Groovy \>= 1.5.0 should work here -->
            <version>3.0.6</version>
            <type>pom</type>
            <scope>runtime</scope>
          </dependency>
        </dependencies>
        <executions>
          <execution>
            <id>read-classpath</id>
            <phase>validate</phase>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>

        </executions>
        <configuration>
          <scripts>
            <script><![CDATA[
                    def file = new File(project.properties.cpfile)
                    /* create a new property named 'cp'*/
                    project.properties.cp = file.getText()
                    println '<<< Retrieving classpath into new property named <cp> >>>'
                    println 'cp = ' + project.properties.cp
                  ]]></script>
          </scripts>
        </configuration>
      </plugin>
Top answer
1 of 16
641

Problems of popular approaches

Most of the answers you'll find around the internet will suggest you to either install the dependency to your local repository or specify a "system" scope in the pom and distribute the dependency with the source of your project. But both of these solutions are actually flawed.

Why you shouldn't apply the "Install to Local Repository" approach

When you install a dependency to your local repository it remains there. Your distribution artifact will do fine as long as it has access to this repository. The problem is in most cases this repository will reside on your local machine, so there'll be no way to resolve this dependency on any other machine. Clearly making your artifact depend on a specific machine is not a way to handle things. Otherwise this dependency will have to be locally installed on every machine working with that project which is not any better.

Why you shouldn't apply the "System Scope" approach

The JAR files you depend on with the "System Scope" approach neither get installed to any repository or attached to your target packages. That's why your distribution package won't have a way to resolve that dependency when used. That I believe was the reason why the use of system scope even got deprecated. Anyway you don't want to rely on a deprecated feature.

The static in-project repository solution

After putting this in your POM file:

<repository>
    <id>repo</id>
    <releases>
        <enabled>true</enabled>
        <checksumPolicy>ignore</checksumPolicy>
    </releases>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <url>file://${project.basedir}/repo</url>
</repository>

for each artifact with a group id of form x.y.z Maven will include the following location inside your project directory in its search for artifacts:

repo/
| - x/
|   | - y/
|   |   | - z/
|   |   |   | - ${artifactId}/
|   |   |   |   | - ${version}/
|   |   |   |   |   | - ${artifactId}-${version}.jar

To elaborate more on this, you can read this blog post.

Use Maven to install to project repository

Instead of creating this structure by hand, I recommend to use a Maven plugin to install your JAR files as artifacts. So, to install an artifact to an in-project repository under repo folder execute:

mvn install:install-file -DlocalRepositoryPath=repo -DcreateChecksum=true -Dpackaging=jar -Dfile=[your-jar] -DgroupId=[...] -DartifactId=[...] -Dversion=[...]

If you'll choose this approach, you'll be able to simplify the repository declaration in the POM file to:

<repository>
    <id>repo</id>
    <url>file://${project.basedir}/repo</url>
</repository>

A helper script

Since executing installation command for each library is kind of annoying and definitely error-prone, I've created a utility script which automatically installs all the JAR files from a lib folder to a project repository, while automatically resolving all metadata (groupId, artifactId, etc.) from names of files. The script also prints out the dependencies XML file for you to copy-paste in your POM file.

Include the dependencies in your target package

When you'll have your in-project repository created, you'll have solved a problem of distributing the dependencies of the project with its source, but since then your project's target artifact will depend on non-published JAR files, so when you'll install it to a repository, it will have unresolvable dependencies.

To beat this problem, I suggest to include these dependencies in your target package. This you can do with either the Assembly Plugin or better with the OneJar Plugin. The official documentation on OneJar is easy to grasp.

2 of 16
508

For throwaway code only

Set scope == system and just make up a groupId, artifactId, and version:

<dependency>
    <groupId>org.swinglabs</groupId>
    <artifactId>swingx</artifactId>
    <version>0.9.2</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/swingx-0.9.3.jar</systemPath>
</dependency>

Note: System dependencies are not copied into the resulting JAR/WAR file (see How to include system dependencies in a WAR file built using Maven)

🌐
GitHub
github.com › raisercostin › eclipse-jarinjarloader › blob › master › README.md
eclipse-jarinjarloader/README.md at master · raisercostin/eclipse-jarinjarloader
A custom class loader based on eclipse code to allow executable jars and custom exclusion of some libraries at runtime. All libraries are bundled inside jar as jars by maven. Some libraries can be filtered out depending on the runtime operating system. The classloader to detect the type of OS and based on that to reconfigure the classpath according to the swt libraries specific to that OS.
Author   raisercostin
🌐
Pdfunit
pdfunit.com › en › documentation › java › install_update › classpath.html
12.3. Setting Classpath in Eclipse, ANT, Maven
To use it with Maven despite this fact, you have to install it into a local or company-wide repository. You can do it with the following command. Change to the directory PDFUNIT_HOME\lib and run this command: mvn install:install-file -Dfile=<PATH_TO>pdfunit-java-VERSION.jar -DpomFile=<PATH...
🌐
GitHub
github.com › markkolich › blog › blob › master › content › entries › maven-add-local-jar-dependency-to-classpath.md
blog/content/entries/maven-add-local-jar-dependency-to-classpath.md at master · markkolich/blog
Download the .jar file to disk, and use mvn to publish the .jar to the lib directory. In this example, I'm publishing the Gagawa library I wrote and open-sourced many years ago. mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \ -Dfile=~/Desktop/gagawa-1.0.1.jar \ -DgroupId=com.hp \ -DartifactId=gagawa \ -Dversion=1.0.1 \ -Dpackaging=jar \ -DlocalRepositoryPath=lib
Author   markkolich
🌐
Coderanch
coderanch.com › t › 456391 › build-tools › Maven-jars-classpath-defining-dependencies
Maven: Using jars in classpath without defining dependencies (Other Build Tools forum at Coderanch)
You'd still have a lot of work to do, since you can't add all the JARs in a directory to the classpath in a generic way, you have to add each one individually. You will have to define them as dependencies to get Maven to pull them into its test classpath. You can avoid adding them to the repository, if you prefer and just make them external references, but I forget the scope value that does that.