Note that there are some explanatory texts on larger screens.

plurals
  1. POJarInputStream: getNextJarEntry always returns null
    primarykey
    data
    text
    <p>I have an <code>I18n</code> helper class that can find out the available <code>Locale</code>s by looking at the name of the files inside the application's Jar.</p> <pre><code>private static void addLocalesFromJar(List&lt;Locale&gt; 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(); // ... } } </code></pre> <p>Currently, this isn't working - <code>jar.getNextJarEntry()</code> seems to always return <code>null</code>. I have no idea why that's happening, all I know is that <code>url</code> is set to <code>rsrc:./</code>. I have never seen that protocol, and couldn't find anything about it.</p> <p>Curiously, this works:</p> <pre><code>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()); } } } </code></pre> <p>In this version, even though there is practically no difference between them, the <code>url</code> is correctly set to the path of the Jar file.</p> <p>Why doesn't the first version work, and what is breaking it?</p> <p><strong>UPDATE</strong>:</p> <p>The working example really only works if I don't use Eclipse to export it. It worked just fine in NetBeans, but in the Eclipse version the URL got set to <code>rsrc:./</code> too.</p> <p>Since I exported it with <code>Package required libraries into generated JAR</code> library handling, Eclipse put its <code>jarinjarloader</code> in my Jar so I can have all dependencies inside it. It works fine with the other settings, but is there any way to make this work independently of them?</p> <hr> <h3>Another question</h3> <p>At the moment, that class is part of my application, but I plan to put it in a separate library. In that case, how can I make sure it will work with separate Jars?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload