How do I access the content of folders inside a jar file?

元气小坏坏 提交于 2019-12-03 17:11:52
OscarRyz

You could get path to the jar file and open it with ZipInputStream list all the files inside that jar.

To know the path of the running jar, try using:

InputStream in = MyClass
                .class
                .getProtectionDomain()
                .getCodeSource()
                .getLocation()
                .openStream();

See also: How do I list the files inside a JAR file?

update

I've compiled an ran your solution and works perfect:

C:\java\injar>dir
 El volumen de la unidad C no tiene etiqueta.
 El número de serie del volumen es: 22A8-203B

 Directorio de C:\java\injar

21/02/2011  06:23 p.m.    <DIR>          .
21/02/2011  06:23 p.m.    <DIR>          ..
21/02/2011  06:23 p.m.             1,752 i18n.jar
21/02/2011  06:23 p.m.    <DIR>          src
21/02/2011  06:21 p.m.    <DIR>          x

C:\java\injar>jar -tf i18n.jar
META-INF/
META-INF/MANIFEST.MF
I18n.class
x/
x/y/
x/y/z/
x/y/z/hola.txt

C:\java\injar>type src\I18n.java
import java.util.*;
import java.net.*;
import java.util.jar.*;
class I18n {
    public static void main( String ... args ) {
        getLocaleListFromJar();
    }
    private static List<Locale> getLocaleListFromJar() {
        List<Locale> locales = new ArrayList<Locale>();
        try {
            URL packageUrl = I18n.class.getProtectionDomain().getCodeSource().getLocation();
            JarInputStream jar = new JarInputStream(packageUrl.openStream());
            while (true) {
                JarEntry entry = jar.getNextJarEntry();
                if (entry == null) {
                    System.out.println( "entry was null ");
                    break;
                }
                String name = entry.getName();
                System.out.println( "found : " +name );
                /*if (resourceBundlePattern.matcher(name).matches()) {
                    addLocaleFromResourceBundle(name, locales);
                }*/
           }
        } catch (Exception e) {
            System.err.println(e);
            return null;
            //return getLocaleListFromFile(); // File based implementation in case resources are not in jar
        }
        return locales;
    }
}


C:\java\injar>java -jar i18n.jar
found : I18n.class
found : x/
found : x/y/
found : x/y/z/
found : x/y/z/hola.txt
entry was null

You can use a JarInputStream to read the contents of a jar file. You'll iterate over the JarEntry objects returned by getNextJarEntry() which have a getName(). If you don't care about any of the jar-specific metadata, such a code-signers or additional Manifest entries, you can stick with the ZipInputStream superclass.

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