java - 创建要在浏览器中启动的 Java 小程序

标签 java applet japplet appletviewer

我正在做一项家庭作业,要求我创建一个在浏览器中运行的基本小程序。我已经知道小程序已被弃用,所以我知道这可能不适用于当前的浏览器。

这是我的 Java 代码:

import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JLabel;


public class SimpleJavaApplet extends JApplet{

    public void paint(Graphics g) {
        g.drawString("Hello World!", 80, 80);
        g.drawString("Blah!", 100, 100);
    }

}

这是我的 HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>A Short Test</title>
</head>
<body>
    <title>Test Applet</title>
    <applet code="SimpleJavaApplet.class" width = "200" height = "50">
    </applet>
    <h1>Test Test Test</h1>
</body>
</html>

我希望发生的是浏览器会启动并显示 HTML,但我唯一认为能够运行的是 AppletViewer。我想知道这是否还有可能,以及我是否应该让我的教授知道这一点。

任何想法将不胜感激。

编辑:为了让事情更清楚,我尝试运行此代码,以便它通过浏览器运行,而不仅仅是 AppletViewer。

最佳答案

Chrome 和 Firefox 都不再支持 NPAPI。

Java and Firefox Browser

Firefox no longer provides NPAPI support (technology required for Java applets) As of September, 2018, Firefox no longer offers a version which supports NPAPI, the technology required to run Java applets. The Java Plugin for web browsers relies on the cross-platform plugin architecture NPAPI, which had been supported by all major web browsers for over a decade. The 64 bit version of Firefox has never supported NPAPI, and Firefox version 52ESR is the last release to support the technology. It is below the security baseline, and no longer supported.

Java and Google Chrome Browser

Chrome no longer supports NPAPI (technology required for Java applets) The Java Plugin for web browsers relies on the cross-platform plugin architecture NPAPI, which had been supported by all major web browsers for over a decade. Google's Chrome version 45 and above have dropped support for NPAPI, and therefore Java Plugin do not work on these browsers anymore.

最终,Java Applet 和 Java Web start 已从 Oracle Java 11 SE 中完全删除。

Java 11 SE Release Notes

The deployment stack, required for Applets and Web Start Applications, was deprecated in JDK 9 and has been removed in JDK 11.

The appletviewer tool was deprecated in JDK 9 (see JDK-8074165) and removed in this release.

因此,如果您使用的是 Java 9 或更低版本,则 appletviewer 可用,或者如果您能够下载并安装古老的 Chrome(小于 45),您可以运行该小程序,但 Java 11 或更高版本没有其他解决方案,只能迁移。

关于java - 创建要在浏览器中启动的 Java 小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59022498/

相关文章:

java - 在 Swing 中使用 KeyListener 的 3 个方法时感到困惑吗?

java - org.hibernate.exception.GenericJDBCException : could not execute statement

java - 如何使用 Streams 将字符串列表转换为 Map

java - Applet 因 JNLP MissingFieldException <jnlp> 停止工作

java - 用于使用智能卡签名的跨平台 Java Applet

java - MouseDragged 和 MouseMoved 在 Java Applet 中不起作用

java - 从 JApplet 关闭 JFrame

java - 如何从 FXML 文件初始化 ListView?

java - 为什么第一个方法调用总是耗时最长?

android - 在 Android Studio 中使用 Processing 会导致内存泄漏