0
点赞
收藏
分享

微信扫一扫

windows 自动启动jar

微言记 03-22 16:00 阅读 2

jar读取目录配置、打包jar后无法获取目录下的配置

jar读取目录配置、打包jar后无法获取目录下的配置。java打成jar包后获取不到配置文件路径。解决项目打成jar包上线无法读取配置文件。打包jar后无法读取resource下的配置文件

场景

需要读取 src/main/resources/mapper下的所有 xml 配置。

前提

代码打包成jar,查看这篇文章:https://lingkang.top/archives/idea-yuan-cheng-shi-diao-jar

原代码

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lingkang
 * @create by 2024/3/18 14:44
 */
public class Test05 {
    public static void main(String[] args) {
        String scanPath="mapper";
        List<String> result=new ArrayList<>();
        // 直接遍历,此时可能是idea、eclipse开发环境。
        URL resource = Test04.class.getClassLoader().getResource("");
        if (resource != null) {
            File file = new File(resource.getPath() + scanPath);
            if (file.listFiles() != null)
                for (File f : file.listFiles()) {
                    result.add(f.getPath());
                }
        }
        // 输出扫描结果
        System.out.println(result);
        // 加载配置
        /*for (String config:result){
            InputStream inputStream = Test04.class.getClassLoader().getResourceAsStream(config);
        }*/
    }
}

image-1710744353286

打包成jar后运行

java -jar mybatis-magic-test.jar

如何打包成jar请查看这篇文章:https://lingkang.top/archives/idea-yuan-cheng-shi-diao-jar

image-1710744419051

这时候获取到的是空值。

远程试调

如何试调jar,查看这篇文章:https://lingkang.top/archives/idea-yuan-cheng-shi-diao-jar

image-1710744615862

可以看到获取到是一个空对象

spring的底层原理

spring的底层实现是:PathMatchingResourcePatternResolver,它分多种情况,但总的来说,通配符时(例如/mapper/*.xml),是通过对jar所有文件进行遍历匹配。

解决打包jar后无法获取目录下的配置

import java.io.File;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author lingkang
 * @create by 2024/3/18 14:14
 */
public class Test04 {
    public static void main(String[] args) throws Exception {
        String scanPath = "mapper";
        List<String> result = scanResource(scanPath);
        // 输出扫描结果
        System.out.println(result);
        // 加载配置
        /*for (String config:result){
            InputStream inputStream = Test04.class.getClassLoader().getResourceAsStream(config);
        }*/
    }

    public static List<String> scanResource(String scanPath) throws Exception {
        URL url = Test04.class.getClassLoader().getResource(scanPath);
        List<String> result = new ArrayList<>();
        if (url != null) {
            JarFile jarFile = null;
            URLConnection con = url.openConnection();
            if (con instanceof JarURLConnection) {
                JarURLConnection jarCon = (JarURLConnection) con;
                jarFile = jarCon.getJarFile();
            } else {
                // 手动接收结果
                String urlFile = url.getFile();
                int separatorIndex = urlFile.indexOf("*/");// tomcat
                if (separatorIndex == -1) {
                    separatorIndex = urlFile.indexOf("!/");// jar
                }
                if (separatorIndex != -1) {
                    // String jarFileUrl = urlFile.substring(0, separatorIndex);
                    String rootEntryPath = urlFile.substring(separatorIndex + 2);  // both separators are 2 chars
                    jarFile = new JarFile(rootEntryPath);
                }
            }

            // 遍历
            if (jarFile != null) {
                boolean has = false;
                for (Enumeration<JarEntry> entries = jarFile.entries(); entries.hasMoreElements(); ) {
                    JarEntry entry = entries.nextElement();
                    String entryPath = entry.getName();
                    // 名称匹配,可以是 ant、正则
                    if (entryPath.startsWith(scanPath)) {
                        result.add(entryPath);
                        has = true;
                    } else {
                        if (has) {
                            break;
                        }
                    }
                }
                jarFile.close();
            } else {
                // 直接遍历,此时可能是idea、eclipse开发环境。
                URL resource = Test04.class.getClassLoader().getResource("");
                if (resource != null) {
                    File file = new File(resource.getPath() + scanPath);
                    if (file.listFiles() != null)
                        for (File f : file.listFiles()) {
                            result.add(f.getPath());
                        }
                }
            }
        }
        return result;
    }
}

直接在idea运行,能正常获取到结果

image-1710744982469

打包jar再运行:

image-1710745058003

也能获取到结果,这对框架开发有所帮助。

举报

相关推荐

0 条评论