JarInputStream: getNextJarEntry always returns null

I have a helper class I18nthat can find one available Localeby looking at the file name inside the Jar application.

private static void addLocalesFromJar(List<Locale> locales) throws IOException {
    ProtectionDomain domain = I18n.class.getProtectionDomain();
    CodeSource src = domain.getCodeSource();
    URL url = src.getLocation();
    JarInputStream jar = new JarInputStream(url.openStream());
    while (true) {
        JarEntry entry = jar.getNextJarEntry();
        if (entry == null) {
            break;
        }
        String name = entry.getName();
        // ...
    }
}

Currently this does not work - jar.getNextJarEntry()always returns null. I don’t know why this is happening, all I know is what is urlinstalled on rsrc:./. I have never seen this protocol and could not find anything.

Curiously, this works:

class Main {
    public static void main(String[] args) {
        URL url = Main.class.getProtectionDomain().getCodeSource().getLocation();
        JarInputStream jar = new JarInputStream(url.openStream());
        while (true) {
            JarEntry entry = jar.getNextJarEntry();
            if (entry == null) {
                break;
            }
            System.out.println(entry.getName());
        }
    }
}

In this version, although there is practically no difference between them, it is urlcorrectly set to the path to the Jar file.

Why does the first version not work and what violates it?

UPDATE

, Eclipse . NetBeans, Eclipse URL- rsrc:./.

Package required libraries into generated JAR, Eclipse jarinjarloader Jar, . , ?


, . , , ?

+1
4

ClassLoader jarinjarloader, Eclipse. -, rsrc: URL, jar, jar. URL factory, openStream() null, , .

- , . , , . -, WEB-INF/lib, . , META-INF/MANIFEST.MF , .

+2

jar, I18n. getProtectionDomain . , .

ProtectionDomain domain = I18n.class.getProtectionDomain();
CodeSource src = domain.getCodeSource();
URL url = src.getLocation();

rsrc:./, URL, ( )

, :)

URL url = getClass().getResource(getClass().getSimpleName()+".class");
java.net.JarURLConnection conn = (java.net.JarURLConnection) url.openConnection();
Enumeration<JarEntry> e = conn.getJarFile().entries();
...

!

+1

Eclipse jarinjarloader , , , jar . URL- jar URL- rsrc:.

, . META-INF/locales. ClassLoader.getResources("META-INF/locales"), .

+1

I use System.getProperty ("java.class.path") to get the location of the jar. I do not know if this matters. I have not explored the ProtectDomain path, so I cannot help you, sorry. For multiple jars, just iterate through this jar file.

0
source

Source: https://habr.com/ru/post/1793494/


All Articles