1

I have apple.html in C:/Java1 and apple1.class in C:/Java, I have given path of class file in applet codebase but cannot initialize the applet.

Here is code

apple.html in C:/Java1

<applet code="apple.class" codebase="file:/C:/Java/" alt="Applet not printed value" width="200" height="200">
</applet>

apple1.class in c:/Java

import java.awt.*;
import java.awt.*;
import java.awt.Graphics;
import java.applet.*; 
import java.lang.*;

public class apple1 extends Applet  
{
       public void paint(Graphics g) 
       {
           g.drawString("hi...I am", 50, 50); 
       } 
}

when I run code using

appletviewer applet.html

It gives applet not initialized.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    How are you loading the webpage? Are you loading it from a web server or from your local file system? – Code-Apprentice Oct 05 '13 at 18:11
  • 1
    My untested guess is that you should use `"../Java"`. – Code-Apprentice Oct 05 '13 at 18:12
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Oct 05 '13 at 18:35
  • @Code-Guru I reckon your guess should be an answer. – Andrew Thompson Oct 05 '13 at 18:36

2 Answers2

1

The codebase is wrong use like

codebase="file:///C:/Java/"

EDIT:

The class name allso need to change

code="apple.class"

but the class name in java file is apple1.

also the code in the paint method doesn't call super.paint(), and don't ask why nothing appears in the browser. But you didn't use the browser yet, so it may be in the future.

0

Try codebase="../Java". This will give a path relative to the location of the HTML page.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268