springboot 本地调试没问题,打包运行报错原因

2023-05-16

1、如果引用了本地jar包或者so库,.dll库等文件,需要在打包的时候都加载进去。

如下图:本地正常,打包的时候谨记,需要打包进去,怎么验证是否打包成功呢?我们继续看打包后的图片。

把jar包后缀改成zip 格式的,打开压缩文件,框内路径,查看libs下的包是否在里面可以找到。如果可以找到就是打包进去了,找不到的话,就是没打包进去,稍后我们再说怎么打包进去。

动态库也打包进去了。

 

动态库打包进去方式如下:


import org.opencv.core.Core;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Arthur.yu
 * @date 2021/12/8 0008
 */
@Configuration
public class NativeConfig {
    static {

    }

    @Bean
    public void loadLib() {
        //根据操作系统判断,如果是linux系统则加载c++方法库
        String systemType = System.getProperty("os.name");
        String ext = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll" : ".so";
//        if (ext.equals(".so")) {
            try {
                NativeLoader.loader("native");
            } catch (Exception e) {
                System.out.println("加载so库失败");
            }
//        }else {
//            System.out.println("load --- dll === sucess");
//        }
        System.out.println("loaded");
    }
}

import java.io.*;
import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author Arthur.yu
 * @date 2021/12/8 0008
 */
public class NativeLoader {
    /**
     * 加载项目下的native文件,DLL或SO
     *
     * @param dirPath 需要扫描的文件路径,项目下的相对路径
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public synchronized static void loader(String dirPath) throws IOException, ClassNotFoundException {
        Enumeration<URL> dir = Thread.currentThread().getContextClassLoader().getResources(dirPath);
        // 获取操作系统类型
        String systemType = System.getProperty("os.name");
        //String systemArch = System.getProperty("os.arch");
        // 获取动态链接库后缀名
        String ext = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll" : ".so";
        while (dir.hasMoreElements()) {
            URL url = dir.nextElement();
            String protocol = url.getProtocol();
            if ("jar".equals(protocol)) {
                JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection();
                JarFile jarFile = jarURLConnection.getJarFile();
                // 遍历Jar包
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry jarEntry = entries.nextElement();
                    String entityName = jarEntry.getName();
                    if (jarEntry.isDirectory() || !entityName.startsWith(dirPath)) {
                        continue;
                    }
                    if (entityName.endsWith(ext)) {
                        loadJarNative(jarEntry);
                    }
                }
            } else if ("file".equals(protocol)) {
                File file = new File(url.getPath());
                loadFileNative(file, ext);
            }

        }
    }

    private static void loadFileNative(File file, String ext) {
        if (null == file) {
            return;
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File f : files) {
                    loadFileNative(f, ext);
                }
            }
        }
        if (file.canRead() && file.getName().endsWith(ext)) {
            try {
                System.load(file.getPath());
                System.out.println("加载native文件 :" + file + "成功!!");
            } catch (UnsatisfiedLinkError e) {
                System.out.println("加载native文件 :" + file + "失败!!请确认操作系统是X86还是X64!!!");
            }
        }
    }

    /**
     * @throws IOException
     * @throws ClassNotFoundException
     * @Title: scanJ
     * @Description 扫描Jar包下所有class
     */
    /**
     * 创建动态链接库缓存文件,然后加载资源文件
     *
     * @param jarEntry
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private static void loadJarNative(JarEntry jarEntry) throws IOException, ClassNotFoundException {

        File path = new File(".");
        //将所有动态链接库dll/so文件都放在一个临时文件夹下,然后进行加载
        //这是应为项目为可执行jar文件的时候不能很方便的扫描里面文件
        //此目录放置在与项目同目录下的natives文件夹下
        String rootOutputPath = path.getAbsoluteFile().getParent() + File.separator;
        String entityName = jarEntry.getName();
        String fileName = entityName.substring(entityName.lastIndexOf("/") + 1);
        System.out.println(entityName);
        System.out.println(fileName);
        File tempFile = new File(rootOutputPath + File.separator + entityName);
        // 如果缓存文件路径不存在,则创建路径
        if (!tempFile.getParentFile().exists()) {
            tempFile.getParentFile().mkdirs();
        }
        // 如果缓存文件存在,则删除
        if (tempFile.exists()) {
            tempFile.delete();
        }
        InputStream in = null;
        BufferedInputStream reader = null;
        FileOutputStream writer = null;
        try {
            //读取文件形成输入流
            in = NativeLoader.class.getResourceAsStream(entityName);
            if (in == null) {
                in = NativeLoader.class.getResourceAsStream("/" + entityName);
                if (null == in) {
                    return;
                }
            }
            NativeLoader.class.getResource(fileName);
            reader = new BufferedInputStream(in);
            writer = new FileOutputStream(tempFile);
            byte[] buffer = new byte[1024];

            while (reader.read(buffer) > 0) {
                writer.write(buffer);
                buffer = new byte[1024];
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (in != null) {
                in.close();
            }
            if (writer != null) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            System.out.println("path :" + tempFile.getPath());
            System.load(tempFile.getPath());
            System.out.println("加载native文件 :" + tempFile + "成功!!");
        } catch (UnsatisfiedLinkError e) {
            System.out.println("加载native文件 :" + tempFile + "失败!!请确认操作系统是X86还是X64!!!");
        }
    }
}

运行的时候,会在jar包所在目录生成一个native文件夹,里面放的就是动态库

下面我们看一下本地jar包怎么打进去:

1、jar包存放路径

2、pom 文件引用一下 

        <dependency>
            <groupId>org.opencv</groupId>
            <artifactId>opencv</artifactId>
            <version>0.0.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/opencv-343.jar</systemPath>
        </dependency>

 3、pom打包配置

<build>
        <plugins>
            <!--参考文章:https://blog.csdn.net/liupeifeng3514/article/details/80236077-->
            <plugin>
                <!-- 指定maven编译的jdk版本,如果不指定,maven3默认用jdk 1.5 maven2默认用jdk1.3 -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <!-- 源代码使用的JDK版本 -->
                    <source>1.8</source>
                    <!-- 需要生成的目标class文件的编译版本 -->
                    <target>1.8</target>
                    <!-- 字符集编码 -->
                    <encoding>UTF-8</encoding>
                    <!-- 跳过测试 -->
                    <skip>true</skip>
                    <compilerArguments>
                        <extdirs>${project.basedir}/libs</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>${project.basedir}/libs/</directory>
                <targetPath>BOOT-INF\lib</targetPath>
               <includes>
                      <include>**/*.jar</include>
                  </includes>
            </resource>
            <resource>
                <directory>${project.basedir}/libs</directory>
            </resource>
        </resources>
    </build>

 好了。动态库跟jar包都打包进去了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

springboot 本地调试没问题,打包运行报错原因 的相关文章

随机推荐

  • FreeRTOS系列-- heap_4.c内存管理分析

    FreeRTOS系列 heap 4 c内存管理分析 heap 4 c简介理解heap 4 c的关键点图示heap 4 c内存申请过程图示heap 4 c内存合并过程内存初始化源码分析内存申请源码分析内存释放分析空闲块内存合并源码分析 hea
  • Cordova概述

    Cordova Apache Cordova is an open source mobile development framework It allows you to use standard web technologies HTM
  • Openstack学习(增加卷迁移限速)

    声明 xff1a 本博客欢迎转发 xff0c 但请保留原作者信息 博客地址 xff1a http blog csdn net halcyonbaby 内容系本人学习 研究和总结 xff0c 如有雷同 xff0c 实属荣幸 xff01 Ope
  • dht11 新手原理详解(附代码)

    dht11详解 dht11原理 简介 DHT11作为一款低价 入门级的温湿度传感器 xff0c 常用于我们的单片机设计实例中 它应用专用的数字模块采集技术和温湿度传感技术 xff0c 确保产品具有极高的可靠性与卓越的长期稳定性 传感器包括一
  • git推送报错 Your branch is ahead of 'origin/master' by 1 commit

    当出现no changes added to commit use git add and or git commit a git commit之后 xff0c 用git status xff0c 打印信息为 xff1a Your bran
  • ubuntu如何降级gcc

    ubuntu版本 xff1a ubuntu 18 04 安装指定版本的gcc g 43 43 xff0c 然后做如下链接 sudo apt get install gcc 4 5 g 43 43 4 5 cpp 4 5 gcc 4 5 mu
  • arm-linux开发环境之(busybox-ls命令)终端显示颜色

    在开发板终端中输入ls命令后终端文件夹和文件显示颜色 linux主机 xff1a ubuntu 12 04 交叉编译器 xff1a gcc version 4 6 2 20110630 prerelease 开发板kernel xff1a
  • 活体识别5:论文笔记之FeatherNets

    说明 这篇文章是这次比赛的第三名 xff1a ChaLearn Face Anti spoofing Attack Detection Challenge 64 CVPR2019 xff0c 此次比赛项目是人脸防欺诈攻击检测 论文标题 xf
  • c++中使用dlopen加载动态库中带类参数的函数

    说明 我一直都知道dlopen的大概用法 但是dlopen毕竟是c语言的函数 xff0c 能否加载带c 43 43 类型传参的函数 xff0c 我有点不确定 今天有空验证了下 xff0c 是可以的 extern C 只影响了函数在动态库中的
  • 将pytorch的pth文件固化为pt文件

    说明 我参考了一个开源的人像语义分割项目mobile phone human matting xff0c 这个项目提供了预训练模型 xff0c 我想要将该模型固化 xff0c 然后转换格式后在嵌入式端使用 该项目保存模型的代码如下 xff1
  • ARM汇编1:如何在C语言中使用汇编

    如何在C语言中使用汇编语言 我最近对ARM的NEON编程有兴趣 xff0c 主要是为了想学习一些矩阵计算加速相关的知识 但是我又不想写纯粹的汇编语言 xff0c 我想在C语言中嵌入汇编来使用 经过检索学习 xff0c 我找到两种可行的方式
  • EEPROM和flash的区别

    之前对各种存储器一直不太清楚 xff0c 今天总结一下 存储器分为两大类 xff1a ram和rom ram就不讲了 xff0c 今天主要讨论rom rom最初不能编程 xff0c 出厂什么内容就永远什么内容 xff0c 不灵活 后来出现了
  • python中import cv2遇到的错误及安装方法

    从x86 64 43 ubuntu14 04 43 python3 5中import cv2 opencv3 3 遇到以下错误 xff1a ImportError libSM so 6 cannot open shared object f
  • 多目标跟踪-MOT16数据集格式介绍

    背景介绍 多目标跟踪的问题是这样的 xff1a 有一段视频 xff0c 视频是由 N 个 连续帧构成的 从第一帧到最后一帧 xff0c 里面有多个目标 xff0c 不断地有出有进 xff0c 不断地运动 我们的目的是对每个目标 xff0c
  • opencv-python的格式转换 RGB与BGR互转

    opencv读取图片的默认像素排列是BGR xff0c 和很多其他软件不一致 xff0c 需要转换 这里转一下国外博客的一个方法 xff0c 基于python语言 BGR to RGB OpenCV image to Matplotlib
  • np.maximum()函数详解——将数组中小于某值的数用0代替

    目标 xff1a 把数组中小于某个值的数都设为0 np max a axis 61 None out 61 None keepdims 61 False 求a中的最大值 np maximum xff1a a b out 61 自定义 a 与
  • python-opencv 使用LBP特征检测人脸

    概述 最近在做人脸检测相关功能 xff0c 目前注意到比较传统 xff08 非深度 xff09 人脸检测特征包括harr和LBP HOG用于行人检测更多些 xff0c opencv包括了这两种特征算法 xff0c 并且相对来说 xff0c
  • 机器视觉特征提取介绍:HOG、SIFT、SURF、ORB、LBP、HAAR

    一 概述 这里主要记录自己的一些感悟 xff0c 不是很系统 想要详细系统的理论 xff0c 请参考文末的 图像处理之特征提取 个人不是专业cv工程师 xff0c 很多细节没有深究 xff0c 描述可能不严谨 在总结物体检测算法之前先把基础
  • win7 wifi 无Internet访问权限或者有限的访问权限

    自己家的无线路由器 xff0c 手机和笔记本都使用正常 xff0c 但是一台新笔记本连上之后总是提示 有限的访问权限 xff0c 无法连公网 网上的很多办法都不管用 xff0c 什么设置静态IP或者重启路由 xff0c 基本都是瞎扯 好在一
  • springboot 本地调试没问题,打包运行报错原因

    1 如果引用了本地jar包或者so库 xff0c dll库等文件 xff0c 需要在打包的时候都加载进去 如下图 xff1a 本地正常 xff0c 打包的时候谨记 xff0c 需要打包进去 xff0c 怎么验证是否打包成功呢 xff1f 我