Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonqian/22d355d17913fd9a7307ee23f1fa2ab1 to your computer and use it in GitHub Desktop.
Save simonqian/22d355d17913fd9a7307ee23f1fa2ab1 to your computer and use it in GitHub Desktop.
classnotfoundexception-after-include-local-jar-to-maven-project

// https://stackoverflow.com/questions/35884928/classnotfoundexception-after-include-local-jar-to-maven-project/50635637#50635637

I have a similar issue today, and stuck me half day to fix it.

For most case, using below is fine for developing.

<dependency>
    <groupId>com.netease</groupId>
    <artifactId>thrift</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/prome-thrift-client-1.0.0.jar</systemPath>
</dependency>

If you put the jar to a correct path, then it's fine to run in both IDEA and Eclipse.

After you deploy the jar to a server or run the jar locally, then it may throws ClassNotFoundException.

If you are using spring-boot, you still need below plugin:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
            <includeSystemScope>true</includeSystemScope>
    </configuration>
</plugin>

After run mvn clean package, then you can find the jar under /BOOT_INF/lib.

Between, if your package is war, then you still need this plugin:

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <configuration>
            <webResources>
                <resource>
                        <directory>lib</directory>
                        <targetPath>BOOT-INF/lib/</targetPath>
                        <!-- <targetPath>WEB_INF/lib/</targetPath> just for none spring-boot project -->
                        <includes>
                            <include>**/*.jar</include>
                        </includes>
                </resource>
            </webResources>
        </configuration>
 </plugin>

-----------------Another Way--------------------

You can using this plugin to replace maven-war-plugin:

 <plugin>  
      <artifactId>maven-compiler-plugin</artifactId>  
       <configuration>  
            <source>1.8</source>  
            <target>1.8</target>  
            <compilerArguments>  
                <extdirs>${project.basedir}/lib</extdirs>  
            </compilerArguments>  
      </configuration>  
</plugin>  

And add the resource:

<resources>  
    <resource>  
        <directory>lib</directory>  
        <targetPath>BOOT-INF/lib/</targetPath>  
        <includes>  
            <include>**/*.jar</include>  
        </includes>  
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>BOOT-INF/classes/</targetPath>
    </resource> 
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment