currently using JBuilder Foundations
task is to display a .gif file in JApplet from my hardrive
the URL is C:\MAGIX
Should the image file be located elsewhere in a specific place?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Applet1 extends JApplet {

    public void init(){
        Container contentPane=getContentPane();
        ImageIcon icon=new ImageIcon("icon.gif");
        JLabel jl=new JLabel("icon",icon,JLabel.CENTER);
        contentPane.add(jl);
    }
}

when running the JApplet, only prints "icon"

Dani AI

Generated

— the observed behavior is almost always the sandbox at work (as noted). Practical fixes are to either bundle the image with the applet or serve it from the same host as the applet. Browsers and the JVM will block arbitrary reads from C:\ for unsigned applets, so relying on an absolute local path is not a long‑term solution.

Place icon.gif inside the applet JAR (or on the classpath) and load it with a resource lookup so it works whether running from a jar or from an IDE:

URL imgURL = getClass().getResource("/icon.gif"); // put icon.gif at jar root
if (imgURL == null) {
    System.err.println("icon.gif not found in JAR");
} else {
    ImageIcon icon = new ImageIcon(imgURL);
    getContentPane().add(new JLabel("icon", icon, JLabel.CENTER));
}

If the image will be delivered by the web server alongside the applet HTML, load it relative to the applet codebase:

Image img = getImage(getCodeBase(), "icon.gif"); // image served by same host
ImageIcon icon = new ImageIcon(img);
getContentPane().add(new JLabel("", icon, JLabel.CENTER));

For debugging, follow ’s suggestion and open the Java console or run with appletviewer so you can see stack traces and missing‑resource messages. If you must access local files, the jar must be signed and the user must grant permissions (modern browsers and JVMs often block signed applets too). Note: applets are obsolete in current browsers — for new work convert to a Swing application, a packaged Java app, or a web technology that browsers support.

Recommended Answers

All 2 Replies

applet security restrictions prevent you from reading anything from your harddrive.
You can only read files from network addresses on the same server (or more correctly (virtual) host) as the one where the applet resides.
As the applet is in your case not coming from a server it can't read anything at all.
Maybe if you place the applet and file in the same directory you can access it, but that would be due to VM specific laxness rather than official policy.

I've never dealt with icons on an applet, but whenever I wanted to draw an image onto the applet I never had a problem if I put it in the same file. Why don't you check the java console and see if it's a security restriction or if it's a FileNotFoundException.

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.