如何在浏览器中 运行 Java 小程序(使用 CMD)

How to run Java applets in browser (with CMD)

我知道,现在没有人使用小程序了,但我的本科学位仍然在教学大纲中有这个!!

这是我的HelloWorld.java文件代码

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

这是我的Hello.html文件代码

<html>
    <body>
        Here is the output of my program:
        <applet code="HelloWorld.class" width="150" height="25"></applet>
    </body>
</html>

两个文件都保存到桌面。

这是我所做的:

  1. 打开CMD
  2. 输入cd desktop
  3. 输入javac HelloWorld.java
  4. 在 Chrome、Opera、Firefox、Microsoft Edge
  5. 中打开 Hello.html 文件

None的浏览器运行小程序!!普通 Java (AWT) 程序 运行 使用 CMD 而不是 applet。

JDK & 我用的JRE版本分别是jdk1.8.0_201 & jre1.8.0_201.

使用以下命令:

/*
<html>
<applet code="HelloWorld.class" CodeBase="" width=300 height=400></applet>
</html>
*/

另存为"HelloWorld.html"

c:\>javac HelloWorld.java
c:\>appletviewer HelloWorld.html