Hi.

I have a problem with launching my application as applet. The game runs fine as an application.

my applet code:

package net.viped.breakout;

import java.applet.Applet;
import java.awt.GridLayout;

public class GameApplet extends Applet
{
  Core core = new Core();

  public void init() {
    setLayout(new GridLayout());

    add(this.core);
  }

  public void start() {
    core.start();
  }

  public void stop() {
    core.stop();
  }
}

error:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at net.viped.breakout.ImageLoader.<init>(ImageLoader.java:14)
    at net.viped.breakout.Core.<init>(Core.java:66)
    at net.viped.breakout.GameApplet.<init>(GameApplet.java:8)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
    at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
    at sun.applet.AppletPanel.run(AppletPanel.java:368)
    at java.lang.Thread.run(Thread.java:680)

Code causing error:

        public ImageLoader() {
            try {
                sprites = ImageIO.read(new File("res/Sprites.png"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

Dani AI

Generated

The exception means ImageIO couldn't open a local file. Two related problems are visible from your trace: (a) the loader uses a File-based path (which fails inside the applet sandbox and is sensitive to the working directory), and (b) the Core/ImageLoader work is happening during GameApplet construction (field initialization), so it runs before the applet context is available. That combination is why it works as an application but not as an applet.

Fixes that reliably work in applets (pick one):

  • Bundle the images in the JAR and load them from the classpath. Example (run from a component or class in the same package):
InputStream in = getClass().getResourceAsStream("/net/viped/breakout/res/Sprites.png");
if (in == null) throw new IOException("resource not found");
BufferedImage sprites = ImageIO.read(in);
in.close();
  • Serve images from the web page and load via the applet codebase (use this from an Applet method so getCodeBase() is valid):
URL url = new URL(getCodeBase(), "res/Sprites.png");
BufferedImage sprites = ImageIO.read(url);
  • If you really need local file access, the applet must be signed and granted permissions — not recommended unless necessary.

Practical tips and troubleshooting

  • Move resource loading out of field initializers. Instantiate Core inside init() and pass the Applet or a URL so code can call getCodeBase() or getResourceAsStream when the applet context exists. Example: create Core in init(), not in a field declaration.
  • Add a quick existence check (getResource or getResourceAsStream returning null) and log the resolved URL to confirm the path is correct.
  • was right to suspect sandbox/security issues; is incorrect that you must override every lifecycle method — only override the ones you use. ’s question about sprite display is relevant: after loading use a BufferedImage and draw it from paintComponent/paint to avoid flicker.

These changes eliminate reliance on the local filesystem and make the applet robust in browser/appletviewer environments.

Recommended Answers

All 4 Replies

Hi,

I'm assuming that the Java Content Security Policy is not allowing the applet to read the file. ( if you are running the application through appletviewer etc. )

Or do you have the images and applet bundled in a jar file?

can you tell me how you are going to display sprites on your applet?

Maybe this would help

i think whenever you are extending Applet class to any other class,
You have to override all the applet life cycle methods in child(inherited) class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.