I am trying to learn java, and I am at an impasse in mastering the applets. I teach by the book and there is such code in the example, but I’m confused by the HTML code (Ie, it is simply inserted into the multi-line comment block after imports) right in the Java code (This is exactly what is shown in the book)

import java.awt.*; import java.applet.*; `/* <applet code = "main" width = 100 height=100></applet> */` class Main extends Applet{ public void init(){ } public void start(){ } public void stop(){ } public void destroy(){ } public void paint(Graphics g){ g.drawString("Hello World", 10,10); } } 

But it does not work. An error is displayed:

Error: Main method not found in class MyPackage.Main, please define the main method as:
public static void main (String [] args) or a JavaFX application class must extend javafx.application.Application

  • What is the code writing? In eclipse there is a special configuration for running applets, in other ide there should be analogues. In general, applets are outdated, you can scroll through the section in the book (or take a newer book) - zRrr
  • I use IntelliJ IDEA Community Edition. Tried to find, but did not find. - Roman Afanasenko

1 answer 1

A comment containing the html-code is needed so that the applet can be launched by the appletviewer utility, which is part of the JDK and intended to facilitate testing of applets. But, first, the class must be public, otherwise it will not work. Secondly, the case of the letters in the class name is significant.

 import java.awt.*; import java.applet.*; /* <applet code = "Main" width=100 height=100></applet> */ public class Main extends Applet { public void init() {} public void start() {} public void stop() {} public void destroy() {} public void paint(Graphics g) { g.drawString("Hello World", 10, 10); } } 

Compile the applet

 $ javac Main.java 

And run

 $ appletviewer Main.java 

Just keep in mind that since 2015, many browsers have stopped supporting them. Since September 2017, Oracle has declared the technology obsolete. Most likely, by the end of the year or next, it will be completely removed from Java.