Note that there are some explanatory texts on larger screens.

plurals
  1. POJava applet can't find resources in jar if run locally
    text
    copied!<p>Problem: Java applet can't load resources located inside its jar when run locally on windows platforms. The same applet can load the resources if it is launched from a web server instead of launched locally or if it's launched locally on a linux system. In all cases the applet is launched using an applet tag.</p> <p>Steps to reproduce</p> <p>1) Build applet class code below and create a jar containing the following:</p> <blockquote> <ul> <li>TestApplet.class</li> <li>iconimg.png</li> <li>test.html</li> <li>META-INF folder (standard manifest with one line: "Manifest-Version: 1.0")</li> </ul> </blockquote> <p>Here's a link to the image png file I used: <a href="http://flexibleretirementplanner.com/java/java-test/iconimg.png" rel="nofollow">http://flexibleretirementplanner.com/java/java-test/iconimg.png</a></p> <p>The file test.html has one line:</p> <blockquote> <pre><code>&lt;h1&gt;Text from test.html file&lt;/h1&gt; </code></pre> </blockquote> <p>2) create launch.html in same folder as test.jar as follows:</p> <pre><code>&lt;html&gt;&lt;center&gt;&lt;title&gt;Test Applet&lt;/title&gt;&lt;applet archive = "test.jar" code = "TestApplet.class" name = "Test Applet" width = "250" height = "150" hspace = "0" vspace = "0" align = "middle" mayscript = "true" &gt;&lt;/applet&gt;&lt;/center&gt;&lt;/html&gt; </code></pre> <p>3) With test.jar in the same local folder as launch.html, click on launch.html</p> <p>4) Notice that getResource() calls for imgicon.png and test.html both return null.</p> <p>5) Upload launch.html and test.jar to a web server and load launch.html and notice that the resources are found.</p> <p>TestApplet.java</p> <pre><code>import java.applet.AppletContext; import java.io.IOException; import java.net.URL; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JEditorPane; import javax.swing.JLabel; import javax.swing.JPanel; public class TestApplet extends JApplet { public TestApplet() { try { jbInit(); } catch (Exception ex) { ex.printStackTrace(); } } public void init() { JPanel topPanel = new JPanel(); JLabel iconLabel; URL url = TestApplet.class.getClassLoader().getResource("iconimg.png"); if (url != null) iconLabel = new JLabel(new ImageIcon(url)); else iconLabel = new JLabel("getResource(iconimg.png)==null"); topPanel.add(iconLabel); URL url2; url2 = TestApplet.class.getClassLoader().getResource("test.html"); if (url2 == null) { JLabel errorLabel = new JLabel("getResource(test.html) == null"); topPanel.add(errorLabel); } else { try { JEditorPane htmlPane = new JEditorPane(url2); topPanel.add(htmlPane); } catch (IOException ioe) { System.err.println("Error displaying " + url2); } } getContentPane().add(topPanel); } private void jbInit() throws Exception { } } </code></pre>
 

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