一、问题描述

  最近开发了一个springboot程序,需要依赖第三方jar包,这个jar包无法直接通过pom远程仓库下载,需要从自己本地引入,于是配置pom文件如下:将本地jar包引入工程,systemPath为jar所在的本地路径

<!--第三方jar包引入-->
<dependency>
  <groupId>com.hikvision.js</groupId>
  <artifactId>data-sdk-bms</artifactId>
  <version>2.0.3</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/data-sdk-bms-2.0.3.jar</systemPath>
</dependency>

在打包时将本地引入的jar引入打进lib文件夹下,如果没有如下配置,jar无法打入lib目录

<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<scope>system</scope>
</dependencySet>
</dependencySets>

然后使用maven打包,首先部署到windows机器上,启动程序,运行正常,然后将程序部署到linux环境上,运行报如下错误:

[WARN ] [2020-09-09 09:28:15] [org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.hikvision.js.deployalarmtool.DeployalarmtoolApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/hikvision/bms/caller/BmsMessage.class] cannot be opened because it does not exist
[ERROR] [2020-09-09 09:28:15] [org.springframework.boot.SpringApplication.reportFailure(SpringApplication.java:771)] Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.hikvision.js.deployalarmtool.DeployalarmtoolApplication]; nested exception is java.io.FileNotFoundException: class path resource [com/hikvision/bms/caller/BmsMessage.class] cannot be opened because it does not exist
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:183)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com.hikvision.js.deployalarmtool.DeployalarmtoolApplication.main(DeployalarmtoolApplication.java:14)
Caused by: java.io.FileNotFoundException: class path resource [com/hikvision/bms/caller/BmsMessage.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:50)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:102)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:89)
at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:76)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:701)
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getInterfaces(ConfigurationClassParser.java:879)
at org.springframework.context.annotation.ConfigurationClassParser.processInterfaces(ConfigurationClassParser.java:368)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:325)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:247)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:192)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:297)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:247)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:200)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:169)
... 13 more

nested exception is java.io.FileNotFoundException: class path resource [com/hikvision/bms/caller/BmsMessage.class] cannot be opened because it does not exist,显然是com/hikvision/bms/caller/BmsMessage.class这个类不存在,而这个类正是引入的本地的那个jar包中的,检查lib目录发现jar是存在的,并且权限也正常,这就很奇怪,一时间束手无策。

排查无果,又重新打包,发现一个maven的warning日志:

[WARNING] 'dependencies.dependency.systemPath' for com.hikvision.js:data-sdk-bms:jar should not point at files within the project directory, ${project.basedir}/lib/data-sdk-bms-2.0.3.jar will be unresolvable by dependent projects @ line 113, column 25

于是我便从这个warning入手进行排查,果然warning解决,问题也得到了解决。

二、解决方法

本地第三方jar包引入方法:

<!--第三方jar包引入-->
<dependency>
<groupId>com.hikvision.js</groupId>
<artifactId>data-sdk-bms</artifactId>
<version>2.0.3</version>
</dependency>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-install-plugin</artifactId>
  <version>2.5.2</version>
  <executions>
    <execution>
      <id>install-data-sdk-bms</id>
      <phase>clean</phase>
      <configuration>
        <file>${project.basedir}/lib/data-sdk-bms-2.0.3.jar</file>
        <repositoryLayout>default</repositoryLayout>
        <groupId>com.hikvision.js</groupId>
        <artifactId>data-sdk-bms</artifactId>
        <version>2.0.3</version>
        <packaging>jar</packaging>
        <generatePom>true</generatePom>
      </configuration>
    <goals>
      <goal>install-file</goal>
    </goals>
   </execution>
  </executions>
</plugin>

按上面的方式配置pom,打包正常,windows和linux环境下运行也正常。

springboot服务引入外部jar包在windows运行正常,在linux环境上无法加载到引入jar包的类的更多相关文章

  1. 页面引入外部字体ttf,如何提取所需要的ttf字体或者加载过慢的解决方法-1127更新

    最近几天编写手机端的页面之后,文中需要华文行楷字体,在网上下载后,引入到了自己的前端页面,以为没有什么事了,继续码代码 @font-face { font-family:huawen; src: ur ...

  2. SpringBoot在启动时的多环境配置以及加载顺序

    通常我们在开发完成一个SpringBoot项目时,总是要打包部署的. 在启动SpringBoot应用时,我们常常会使用命令java -jar xxx.jar来启动这个服务. 命令java -jar 除 ...

  3. Java ClassLoader基础及加载不同依赖 Jar 中的公共类

    转载自:最新内容及最清晰格式请见 http://www.trinea.cn/android/java-loader-common-class/ 本文主要介绍 ClassLoader 的基础知识,Cla ...

  4. 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

    [源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...

  5. Maven编译的时候加载本地路径jar

    大家都知道Maven的依赖是通过pom文件管理的,只要配置了<dependency>,Maven就会从本地仓库 -> 远程仓库 -> 中央仓库获取依赖的jar. 但是如果仓库中 ...

  6. 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据

    [源码下载] 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 绑定 通过实 ...

  7. 未能正确加载“visual C++ package”包

    早上打开360要卸载软件,跳出说系统修复,习惯性的点击修复,结果修复后发现打开vs2012提示“未能正确加载“visual C++ package”包……..”, 重启也一样,google了下,是因为 ...

  8. 在运行jar时自动加载指定的jar包

    初学Java的人经常遇到的一个问题是:如果一个程序依赖某个文件夹下的一堆jar包,那么启动它的时候就需要在java -cp参数后面一个一个的加上jar包的名称,很不方便. 比如主程序类叫Main,在目 ...

  9. 加载依赖的jar包在命令行编译和运行java文件

    在命令里编译和执行java文件,当应用程序需要需要依赖的jar包里面的class文件才能编译运行的时候,应该这样做: 1. 首先是编译过程,在命令行里面执行: (1) javac -classpath ...

随机推荐

  1. ctf平台

    CTF靶场 蓝鲸安全:http://whalectf.xin bugku:https://ctf.bugku.com XCTF攻防世界:https://adworld.xctf.org.cn/ i春秋 ...

  2. Meterpreter后渗透阶段关闭防火墙与杀毒软件

  3. 『无为则无心』Python基础 — 61、Python中的迭代器

    目录 1.迭代的概念 2.迭代器的概念 3.可迭代的对象(Iterable) 4.迭代器对象(Iterator) 5.迭代器的使用体验 (1)基本用法 (2)实际应用 1.迭代的概念 (1)什么是迭代 ...

  4. 在线快速匹配IP

    网址 http://www.bejson.com/othertools/regex/   正则表达式:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}   演示 登录http:// ...

  5. 【C#基础概念】Ineterface 接口的设计原则

    接口设计方式 自顶向下 (如图所示),自底向上(发现类需要结构了就声明一个接口). 接口的作用 用来解耦.继承 接口的本质

  6. 【C#特性】 Attribute 特性

    msdn:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/attributes/creating-c ...

  7. 国产化之虚拟ARM64-CPU安装银河麒麟操作系统

    背景 某个项目需要实现基础软件全部国产化,其中操作系统指定银河麒麟v4,CPU使用飞腾处理器.我本地没有这个国产的处理器,但飞腾是基于ARMv8架构的64位处理器,所以理论上基于这个CPU架构的硬件应 ...

  8. Spring框架第一天(搭建项目)

    Spring框架 1.简介 1.1 Spring是什么 一个开源的框架,是JavaEE开源框架 Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以IoC(Inve ...

  9. Spring系列20:注解详解和Spring注解增强(基础内功)

    有部分小伙伴反馈说前面基于注解的Spring中大量使用注解,由于对Java的注解不熟悉,有点难受.建议总结一篇的Java注解的基础知识,那么,它来了! 本文内容 什么是注解? 如何定义注解 如何使用注 ...

  10. ABP Framework 5.2 RC 发布及新增功能介绍

    ABP Framework 5.2 RC 新增功能 目录 ABP Framework 5.2 RC 新增功能 单层解决方案模板 EF Core 数据库迁移 UI 和 数据库 选项 API 版本控制 源 ...