title: security exception when loading web image in jar
body: <p>I am trying to create a java jar Applet that will run in a browser, download an image from a URL, and display it to the user.
  My implementation is:</p>
  
  <pre><code>try {
       String imageURL = "http://www.google.com/intl/en_ALL/images/logo.gif";
       URL url = new URL(imageURL);
       img = ImageIO.read(url);
     } catch (IOException e) {
       System.out.println(e);
     }  
  </code></pre>
  
  <p>But it gives me a security exception:</p>
  
  <pre><code>java.security.AccessControlException: access denied (java.net.SocketPermission www.google.com:80 connect,resolve)
  </code></pre>
  
  <hr>
  
  <p><strong>Solution:</strong></p>
  
  <p>I have implemented Knife-Action-Jesus's suggestion, and it works <strong>in a web browser</strong> (but not using the applet viewer).</p>
  
  <p><em>Only with the applet viewer</em> I still encounter:</p>
  
  <pre><code>java.security.AccessControlException: access denied (java.net.SocketPermission www.google.com:80 connect,resolve)
  </code></pre>
  
  <p>When loading the web page in a browser, there is a Trust/Deny dialogue box, if I click Trust, then the image shows up.</p>
  
  <p>These are the steps that I'm taking:</p>
  
  <pre><code>ant makejar
  jarsigner -keystore keystore-name -storepass password -keypass password web/LoadImageApp.jar alias-name
  jarsigner -verify -verbose web/LoadImageApp.jar 
  appletviewer web/index.html  ## as mentioned above, this gives a security exception. instead, load the webpage in a browser.
  </code></pre>
  
  <p>The output from jarsigner -verify is:</p>
  
  <pre><code>Warning: The signer certificate will expire within six months.
  
           332 Thu Jan 07 20:03:38 EST 2010 META-INF/MANIFEST.MF
           391 Thu Jan 07 20:03:38 EST 2010 META-INF/ALIAS-NA.SF
          1108 Thu Jan 07 20:03:38 EST 2010 META-INF/ALIAS-NA.DSA
  sm       837 Thu Jan 07 20:03:38 EST 2010 LoadImageApp$1.class
  sm       925 Thu Jan 07 20:03:38 EST 2010 LoadImageApp.class
  sm        54 Wed Jan 06 01:28:02 EST 2010 client.policy
  
    s = signature was verified 
    m = entry is listed in manifest
    k = at least one certificate was found in keystore
    i = at least one certificate was found in identity scope
  
  jar verified.
  </code></pre>
  
  <p>The following is the complete java source code (to emphasize the concept, I removed all that extra exception handling/null checking) :</p>
  
  <pre><code>import java.awt.*;
  import java.awt.image.*;
  import javax.imageio.*;
  import javax.swing.*;
  import java.net.*;
  import java.security.*;
  
  public class LoadImageApp extends JApplet
  {
    private BufferedImage img;
    private final String imageURL = "http://www.google.com/intl/en_ALL/images/logo.gif";
  
    public void init()
    {
      loadImage();
    }
  
    public void paint(Graphics g)
    {
      if (null != img) { g.drawImage(img, 0, 0, null); }
    }
  
    public void loadImage()
    {
      AccessController.doPrivileged(new PrivilegedAction()
      {
        public Object run()
        {
          try
          {
            URL url = new URL(imageURL);
            if (null == url)
            {
              throw new MalformedURLException();
            }
            img = ImageIO.read(url);
          }
          catch (Exception e) { e.printStackTrace(); }
          return null;
        }
      });
    }
  
  }
  </code></pre>