Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JarIndex seems to be useful for applets and networking. It can prevent from loading unnecessary archives.</p> <p>Here we have a so called <em>root jar</em> which includes an <code>INDEX.LIST</code> file and that file includes a mapping from class to library. The classloader will read the file, create an internal hashtable and use this table to determine, where a class can be found. If the library has not been loaded yet, then it will load it to load the class. Otherwise it would have to load all libraries at once just to resolve a single class (because a class name never gives any clue where a class can be found)</p> <p>Once the classloader finds such an index, it trusts that information and will complain with an exception if the information is not true. Say the index tells the classloader that <code>com.example.MyClass</code> can be found inside <code>http://example.com/a.jar</code>, then it will download (if not already done) the jar and only look inside this library. If there's no such class it will <em>not</em> look in different jars (or even download additional jars) but will have a hump with you (and throw an exception).</p> <p>If you encounter such an exception you might be pretty lost. The problem (corrupt INDEX.LIST file) can't be fixed on the consumers side. But because the classloader expects the <code>INDEX.LIST</code> file in the first jar on the classpath, changing the order of libraries in the classpath expression could solve such a problem by disabling the indexer feature.</p> <hr> <p><strong>Further Reading</strong></p> <ul> <li><a href="http://bugs.sun.com/view_bug.do;jsessionid=b8832ba175f6e6737bc1c320fa24b?bug_id=6901992" rel="nofollow noreferrer">Possible InvalidJarIndexException due to bug in sun.misc.JarIndex.merge()</a></li> </ul> <hr> <p><strong>Working Example with ant</strong></p> <p>I created two very simple classes to print Hello world:</p> <pre><code>package test; public class Hello { public static void main(String[] args) { System.out.println(HelloTextFactory.createResponse()); } } </code></pre> <p>and</p> <pre><code>package test; public class HelloTextFactory { public static String createResponse() { return "Hello world"; } } </code></pre> <p>and an ant file (build.xml) to create the archives:</p> <pre><code>&lt;project name="test"&gt; &lt;target name="jar" description="description"&gt; &lt;delete file="main.jar" /&gt; &lt;delete file="factory.jar" /&gt; &lt;jar destfile="factory.jar" includes="test/HelloTextFactory.class" basedir="bin" /&gt; &lt;jar destfile="main.jar" includes="test/Hello.class" basedir="bin" index="true"&gt; &lt;indexjars&gt; &lt;fileset dir="." id="jars"&gt; &lt;include name="factory.jar" /&gt; &lt;/fileset&gt; &lt;/indexjars&gt; &lt;manifest&gt; &lt;attribute name="Main-Class" value="test.Hello" /&gt; &lt;attribute name="Class-Path" value="factory.jar" /&gt; &lt;/manifest&gt; &lt;/jar&gt; &lt;/target&gt; &lt;/project&gt; </code></pre> <p>This build file assumes that the classes have been compiled into the <code>bin</code> folder before running the script.</p> <p>Running the build script creates two jars, main.jar contains an index and <code>java -jar main.jar</code> runs successfully. Then I moved the second class into a different package and started the build again. And again, it created a working application.</p> <p>During experimentation I realized that</p> <ul> <li>it is necessary to force creation of all jars, especially the main jar. If ant rebuilds <code>factory.jar</code> the index will not be updated and may be invalid. That's why I added the <code>delete</code> tasks.</li> <li><code>main.jar</code> has to be created <em>after</em> all other jars are created.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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