1

I had an issue a couple of days ago with Java applets. Here is my code:

Hello.java class file

//Reference the required Java libraries
 import java.applet.Applet; 
 import java.awt.*; 
 import java.io.*;

 //The applet code
  public class Hello extends Applet {

     public static void main(String[] args)  {
       //new Main();
       System.out.println("Hello worlds");
    }
  } 

And the HTML file applet.html

<HTML>
 <HEAD>
 <TITLE> My First Java Applet </TITLE>
 </HEAD>
 <BODY>
 <P>Here's my first Java Applet@@@@: <BR><BR><P>
 <applet code ="Hello.class"  width="200" height ="200"> APPLET </applet>
 </BODY>
 </HTML>

and I had taken the issue,

 "Error,click for details."

and another issue I take when I'm trying to run my applet from command prompt is

C:\Users\Andreas>cd C:\xampp\htdocs\mysite\
C:\xampp\htdocs\mysite>appletviewer applet.html

after I click enter the appletviewer window is opening but does not displaying anything.It just says: "Applet started"

I Have installed the latest version of java and Java SDK, and I also double-check this in java.com.

I'm running this locally.

Can anyone help me? What I'm missing? In other websites which are running java applets its just asking for permission at the top of the browser and that's all.

I'm trying to get this to work in order to write code about multitasking (any other ideas about multitasking for a website?)

Antreas Apostolou
  • 315
  • 2
  • 9
  • 17
  • 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). 3) Applet has no main. – Andrew Thompson Aug 18 '13 at 20:05
  • So you suggest me work with Swing instead of Applet? Does Swing supports multithreating? – Antreas Apostolou Aug 18 '13 at 20:26
  • *"Swing instead of Applet"* That does not make any sense. I suggest you work with Swing instead of AWT, and frame instead of applet. So instead of being `java.applet.Applet` (AWT/applet), the main class should be `javax.swing.JFrame` (Swing/frame). But if you are getting confused over those glaring differences, probably best avoid GUIs for a while yet. – Andrew Thompson Aug 18 '13 at 20:33
  • *"Does Swing supports multithreating?"* Yes, better than AWT too.. – Andrew Thompson Aug 18 '13 at 20:36
  • Thank you so much for the support,i still can not display anything in the browser.I tried one of the examples you have linked me in a previous comment.I have to install anything else to get started with swing? – Antreas Apostolou Aug 18 '13 at 20:48
  • Ensure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show for applets & JWS apps. If there is no output at the default level, raise it and try again. – Andrew Thompson Aug 18 '13 at 21:25

1 Answers1

0

In the Applet class, main() method is not there. Please try to execute the below example:-

import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet{

public void paint(Graphics g){

g.drawString("hello world" , 25, 50);

  }
}

-

<html>
<title>Applet application</title>
<hr>
<applet code="HelloWorldApplet.class" width="320" height="120">

If the browser is not java enabled then this message will be printed.

</applet>
<hr>
</html>
George Brighton
  • 5,131
  • 9
  • 27
  • 36
Praveen Kishor
  • 2,413
  • 1
  • 23
  • 28