0

I'm learning Java and reading this book: https://www.fca.pt/cgi-bin/fca_main.cgi/?op=2&isbn=978-972-722-791-4.

In this book, I have a Java applet exercise. I can run it in Eclipse in appletviewer and works well. but I'm having trouble integrating the applet into HTML.

Here's my java code:

package packageteste;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Date;

public class Relogio extends Applet implements Runnable{


    Date data;
    Thread proc;
    Font f = new Font("TimesRoman", Font.BOLD, 40);

    public void start(){

        proc = new Thread(this);
        proc.start();

    }

    public void stop(){

        proc = null;

    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {

        Thread th = Thread.currentThread();
        while(proc == th){

            data = new Date();

            try{

                th.sleep(500);

            }catch(InterruptedException e){}

            repaint();

        }

    }

    public void paint(Graphics g){

        g.setFont(f);
        g.setColor(Color.GREEN);
        g.drawString(data.toString(),20,60);
    }}

And now here's my html code :

<!DOCTYPE html>
<html>


<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>


<body>
<applet code = "packageteste.Relogio.class" width="700"></applet>
</body>


</html>
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
  • Which class can't be found? – Shotgun Ninja Aug 10 '15 at 15:12
  • 1
    1) Why code an applet? If it is due to the teacher specifying it, 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 use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Aug 11 '15 at 12:03

1 Answers1

1
  • code = "packageteste.Relogio.class" must not include .class
  • If you have your applet built into a .jar file use the archive="..." attribute to tell the browser what .jar it is.
  • If you don't have a .jar make sure the class packageteste.Relogio can be found as Relogio.class in the packageteste directory.

See also here: How to specify correctly codebase and archive in Java applet?

Community
  • 1
  • 1
JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • Note 2 things. 1) An applet that is not digitally signed will have almost no chance of being successfully launched in a modern JRE. To be signed, code needs to be in a Jar. 2) `` besides removing the `.class` as mentioned in the answer, be sure to specify a `height` for the applet. It is a required attribute. – Andrew Thompson Aug 11 '15 at 12:01