Trying to get a properties file from a jar file in another jar file

守給你的承諾、 提交于 2019-12-11 03:05:57

问题


I have an executable jar file (A.jar), built with Eclipse, that relies on other jar files (B.jar) that are included in the jar file, and accessed via the jarinjarloader. While this works to access the java code inside those jar files, when that code calls

InputStream inStream = <Class>.getClassLoader().getResourceAsStream(propFile);

it fails to find the properties file contained in B.jar. The code works when run in Eclipse, so I know the correct properties files are there.

I tried extracting A.jar, then extracting B.jar, and then zipping up the combined files, but this does not work. Looking at the Manifest file, the Main-Class is org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

Is there some change I can make to the Manifest file (say, adding something to Rsrc-Class-Path) that will get this to work? Rsrc-Class-Path includes "./" already, but that clearly isn't letting the properties loader see my properties files.

TIA,

Greg


回答1:


I'd also recommend working around nested jars. I usually just do this in my build.xml to build one jar with all the libraries unpacked into it:

<target name="fatjar" depends="compile">
    <jar destfile="${isamjar}" filesetmanifest="mergewithoutmain">
            <manifest>
                    <attribute name="Main-Class" value="com.xyz.Main"/>
                    <attribute name="Class-Path" value="."/>
            </manifest>
            <fileset dir="./bin"/>
            <fileset dir="." includes="src/**"/>
            <zipfileset excludes="META-INF/*.SF" src="${l}/derby.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${l}/jconn3.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${l}/log4j-1.2.9.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="${l}/${jar4j}"/>
    </jar>




回答2:


Turns out Eclipse has a setting I missed. When Exporting a "Runnable JAR File" you can select "Extract required libraries into generated JAR". while this may give some license issues (depending upon the JARs you're merging in), in my case it worked like a charm, giving me a single (smaller, since there were a lot of duplicates among the various jar files I'd been including) jar that actually worked.



来源:https://stackoverflow.com/questions/6963410/trying-to-get-a-properties-file-from-a-jar-file-in-another-jar-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!