创建 Java 小程序以在浏览器中启动
Creating a Java Applet to Launch in Browser
我正在做一项家庭作业,要求我创建一个 运行 在浏览器中的基本小程序。我已经知道小程序已被弃用,所以我知道这可能不适用于当前的浏览器。
这是我的 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。
我不确定您要做什么,但这里有一些提示。
如果您想测试一些小程序,请安装旧的 jdk 1.6.x 或 Java 8(避免 jdk 7)。
使用 Firefox esr 52.9
不再支持 Chrome 和 Firefox NPAPI。
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 小程序和 Java Web 启动已从 Oracle Java 11 SE 中完全删除。
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 代码:
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。
我不确定您要做什么,但这里有一些提示。 如果您想测试一些小程序,请安装旧的 jdk 1.6.x 或 Java 8(避免 jdk 7)。 使用 Firefox esr 52.9
不再支持 Chrome 和 Firefox NPAPI。
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 小程序和 Java Web 启动已从 Oracle Java 11 SE 中完全删除。
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 或更高版本没有替代解决方案只有迁移。