java打包jar后读取依赖jar包中的文件资源,支持读取jlink打包的模块镜像中读取

java项目通常会打包成jar,jar里面依赖第三方的jar,那如何在我们的程序中读取第三方jar中的资源呢,当然还有一种情况,是从新版jdk通过jlink打包生成的镜像中获取第三方资源

这里提供两种打包方式的获取方法

  1. 普通打包的jar,例如springboot的jar
  2. 模块化打包的程序,例如jlink打包的镜像
java 复制代码
		try {
			//普通jar包中获取
			//jar:file:///jar包具体位置!/包中资源具体位置
//			URL url = new URL("jar:file:///D:/maven/repository/org/bytedeco/ffmpeg/6.1.1-1.5.10/ffmpeg-6.1.1-1.5.10-windows-x86_64-gpl.jar!/org/bytedeco/ffmpeg/windows-x86_64-gpl/ffmpeg.exe");
			
			//模块化中获取
			//jrt:/模块名/包中资源具体位置
			URL url = new URL("jrt:/org.bytedeco.ffmpeg.windows.x86_64.gpl/org/bytedeco/ffmpeg/windows-x86_64-gpl/ffmpeg.exe");
			InputStream openStream = url.openStream();
			System.out.println(openStream.available());
			
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}
		

模块化打包获取第三方jar中资源的另外一种方式

新的jdk支持模块化打包,此时是以jrt协议访问的,同时jdk新增了FileSystem类,可以用来访问第三方jar中的资源

java 复制代码
		FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
        //fs.getPath("modules/模块名", "包中资源具体的位置");
        Path imagePath = fs.getPath("modules/org.bytedeco.ffmpeg.windows.x86_64.gpl", "org/bytedeco/ffmpeg/windows-x86_64-gpl/ffmpeg.exe");
        byte[] bytes = null;
		try {
			bytes = Files.readAllBytes(imagePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
        System.out.println(bytes.length);

通用获取资源方法

java 复制代码
try {
			String jarPathName = "ffmpeg-6.1.1-1.5.10-windows-x86_64-gpl";
			String jrtModuleName = "org.bytedeco.ffmpeg.windows.x86_64.gpl";
			
			ClassLoader systemClassLoader = App.class.getClassLoader();
			Enumeration<URL> systemResources = systemClassLoader.getResources("META-INF/MANIFEST.MF");
			Iterator<URL> iterator = systemResources.asIterator();
			while (iterator.hasNext()) {
				URL next = iterator.next();
				String path = next.toString();
				
				//ffmpeg-6.1.1-1.5.10-windows-x86_64-gpl是普通jar的路径格式,直接以文件名表示
				//org.bytedeco.ffmpeg.windows.x86_64.gpl是jlink打包镜像中以jrt:协议的路径格式,他其实是路径中的模块名
				//通过动态获取App的所有类和第三方依赖jar,进行过滤,再进行拼接第三方jar中资源的具体路径
				if (path.indexOf(jarPathName) >= 0 || path.indexOf(jrtModuleName) >= 0) {
					String replace = path.replace("META-INF/MANIFEST.MF", "");
					
					//jar:file:///jar包具体位置!/包中资源具体位置
					//jrt:/模块名/包中资源具体位置
					replace = replace + "org/bytedeco/ffmpeg/windows-x86_64-gpl/ffmpeg.exe";
					
					System.out.println(replace);
					
					URL url = new URL(replace);
					InputStream openStream = url.openStream();
					System.out.println("ffmpeg:"+openStream.available());
				}

			}
		} catch (java.lang.Exception e) {
			e.printStackTrace();
		}

通过Main方法类的getClassLoader可以获取当前程序所有依赖类和第三方jar,然后通过过滤得到路径,就能获取jar包或者jlink模块镜像中的资源了

相关推荐
qmx_0717 分钟前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战25 分钟前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
编程零零七1 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
技术无疆2 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
2401_858286112 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py2 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy2 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond4 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ5 小时前
Java 23 的12 个新特性!!
java·开发语言·学习