简体   繁体   English

读取另一个jar文件的资源

[英]Reading resources of another jar file

I have three java jar files which can be run independently. 我有三个可以独立运行的Java jar文件。 Each file contains images resource. 每个文件都包含图像资源。 I want each of the programs to be able to read resource from each other program. 我希望每个程序都能从其他程序读取资源。 Let's assume they are in the same directory, when the program "A" is run I want to be able to read resources file inside file "B" or "C". 假设它们在同一目录中,当运行程序“ A”时,我希望能够读取文件“ B”或“ C”中的资源文件。

There may be a more elegant way of doing this with classloaders/etc. 使用classloaders / etc可能会有更优雅的方法。

However, JAR files are simply ZIP files with a different name. 但是,JAR文件只是具有不同名称的ZIP文件。 Assuming you know in advance the location of the folder in the JAR (probably src/main/resources ), you can simply unzip and read from there. 假设您事先知道该文件夹在JAR中的位置(可能是src/main/resources ),则只需解压缩并从中读取即可。

The class ZipEntry will allow you to do this, without needing any extra libraries: https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html ZipEntry类允许您执行此操作,而无需任何其他库: https : //docs.oracle.com/javase/8/docs/api/java/util/zip/ZipEntry.html

Like I say, however, there is likely a better way of doing this. 但是,就像我说的那样,可能会有更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM