Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassNotFoundException after include local jar to maven project

Tags:

java

spring

maven

I have a spring boot project based on maven.

I have to include a local jar that provided by another team.So I follow the accepted answer from :How to include local jar files in Maven project

So I use:

<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>

That works fine at the IDEA. But when I tried to run the package jar with:

  mvn clean package
  java -jar target/prome-data.jar

Stacktrace is like:

....
....
Caused by: java.lang.ClassNotFoundException: com.netease.thrift.client.ThriftRpcClient

Is there anything I am missing

like image 892
JaskeyLam Avatar asked Dec 25 '22 08:12

JaskeyLam


1 Answers

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>
like image 164
Chao Jiang Avatar answered May 11 '23 11:05

Chao Jiang