文章目录
引用的第三方jar的目录结构(示例)
我的lib是在resources目录下的。
引入第三方jar进行dependency使项目内能import
其实用命令行的方式将jar包打到本地仓库,然后dependency也行,但是因为我需要将jar放到linux机器中,总不能每次换环境都要执行一次命令吧,而且jar包很多的时候呢?(你要会docker当我没说。),所以我用了下面的方法。
<dependency>
<groupId>com.fbcds</groupId>//随便写,一个唯一的标识,不能重复
<artifactId>bcpkix-jdk15on-1.57</artifactId>
//artifactId虽然也是随便写,但是后面会用到,所以需要记录下来。
<version>1.0</version>
<scope>system</scope>
<systemPath>${
project.basedir}/src/main/resources/lib/bcpkix-jdk15on-1.57.jar</systemPath>
//systemPath第三方jar在本地的目录,${project.basedir}自动获取
//项目路径
</dependency>
这里只演示了jar中的一个,其他的省略但是注意
groupId和artifactId不能重复,artifactId和version后面会用到,所以artifactId最好用jar包的名字
本地编译
本地idea中编译的时候会classnotfound
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerArguments>
<extdirs>lib/bcprov-jdk15on-1.57.jar</extdirs>
<extdirs>lib/com.ibm.mq.allclient-9.0.4.0.jar</extdirs>
<extdirs>lib/javax.jms-api-2.0.1.jar</extdirs>
<extdirs>lib/javax.servlet-api-3.1.0.jar</extdirs>
<extdirs>lib/bcpkix-jdk15on-1.57.jar</extdirs>
</compilerArguments>
</configuration>
</plugin>
加入相应的extdirs即可 ,比如我这里的lib/bcprov-jdk15on-1.57.jar
第三方jar一起打到jar中
问题:上一步本地编译没问题了,终端到生成的jar的目录,java -jar,还是报错classnotfound
比如我的错误:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/ibm/mq/MQException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java