Java applet cannot be run

1.9k Views Asked by At

I am trying to run a java applet with instruction on the textbook. The code is as below.But when I run the appletviewer in cmd the appletviewer is not running. The CMD command is as below

import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;

public class HelloAgainApplet extends java.applet.Applet{

    Font f = new Font ("TimesRoman" ,Font.BOLD,36);

    public void paint(Graphics g){
        g.setFont(f);
        g.setColor(Color.red);
        g.drawString("HelloAgain!", 5, 50);
    }
}

C:\HTML>appletviewer -debug HelloAgainApplet.class Initializing jdb
 ...
 > run run sun.applet.Main HelloAgainApplet.class VM start exception: VM initialization failed for: C:\Program Files
 (x86)\Java\jdk1.8.0_66\jre\bin\java -Djava.class.path=C:\Program Files
 (x86)\Java\jdk1.8.0_66\jre\phony -Xdebug
 -Xrunjdwp:transport=dt_shmem,address=javadebug66917,suspend=y sun.applet.Main HelloAgainApplet.class

 Error: Could not find or load main class Files

 Fatal error: Target VM failed to initialize.

May I know why the error occurs? Is it that I had not set the classpath for the java appletviewer?

1

There are 1 best solutions below

0
cachius On

You must pass an html file that references your class file as argument to appletviewer:

Sample "Applet.html"
<applet code="HelloAgainApplet" width=200 height=60></applet>

Then start via

appletviewer -debug Applet.html

and run the applet with a simple run command. The CLI should look like this:

appletviewer.exe -debug Applet.html
Initializing jdb ...
> run
run sun.applet.Main Applet.html
...