Embedding Java Applet into .html file
Making applets work across a wide range of browsers is surprisingly hard. The tags weren't properly standardized in the early days, so Internet Explorer and Mozilla went separate directions.
Sun developed a generic JavaScript to handle all the specific browser quirks, so that you don't have to worry about browser compatibility.
Add this to your <head>
section:
<script src="//www.java.com/js/deployJava.js"></script>
And this to <body>
section:
<script>
var attributes = {codebase: 'http://my.url/my/path/to/codebase',
code: 'my.main.Applet.class',
archive: 'my-archive.jar',
width: '800',
height: '600'};
var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
var version = '1.5'; // JDK version
deployJava.runApplet(attributes, parameters, version);
</script>
See Java™ Rich Internet Applications Deployment Advice for a detailed explanation of the script and all the possible options.
Embedding JAR file into HTML?
An Applet is what you want...put the following inside your html..
<APPLET ARCHIVE="yourfile.jar" CODE="yourApplet.class" WIDTH=400 HEIGHT=200>
</APPLET>
Of course for this, your jar file must contain yourApplet.class
Unable to open applet in any of the browser
Modern Browsers does not support java applets; as per below post on official Java website:
https://www.java.com/en/download/faq/chrome.xml
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.
Also, for edge, this stackover flow post indicates the same...
Is it possible/how to embed and access HTML Files in a JAR?
File manual = new File(getClass().getResource("/manual/help.html").toURI());
That is where it goes wrong. Java cannot create a File
object from an embedded-resource
Keep it as an URL
and use that for setPage(..)
.
As to the more general problem.
HTML from a Jar file that links resources (e.g. CSS or images) by relative references will work just fine.
E.G.
This example loads HTML (that has a relative reference to an image) from a Jar.
import javax.swing.*;
import java.net.URL;
class ShowHtml {
public static void main(String[] args) {
final String address =
"jar:http://pscode.org/jh/hs/object.jar!/popup_contents.html";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
URL url = new URL(address);
JEditorPane jep = new JEditorPane(url);
JFrame f = new JFrame("Show HTML in Jar");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JScrollPane(jep));
f.pack();
f.setSize(400,300);
f.setLocationByPlatform(true);
f.setVisible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
}
Screenshot
HTML
The HTML that is being loaded.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--
* Copyright (C) 1997 Sun Microsystems, Inc
* All rights reserved.
* Notice of copyright on this source code
* product does not indicate publication.
*
* RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by
* the U.S. Government is subject to restrictions as set forth
* in subparagraph (c)(1)(ii) of the Rights in Technical Data
* and Computer Software Clause at DFARS 252.227-7013 (Oct. 1988)
* and FAR 52.227-19 (c) (June 1987).
*
* Sun Microsystems, Inc., 2550 Garcia Avenue,
* Mountain View, California 94043.
*
-->
<HTML>
<HEAD>
<TITLE>
Editing Project Attributes
</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<IMG SRC="images/popup_icon.gif" width="24" height="24"> <b>Popup Window</b>
<p>
Popup windows appear near the location from which they are
activated. They are not contained in frames and thus
cannot be resized or moved by the user. Popups are
dismissed by clicking anywhere in the help viewer.
<p>
Popup windows can be activated by clicking on a text object,
graphic object, or JComponent button. All three examples are
included in this demo.
<p>
<A HREF="popup_contents2.html">More...</A>
</body>
</html>
E.G. 2
For dynamically created HTML, the JRE will probably use the class file's location as the presumed location of the HTML. But to remove all doubt, we can specify the base
element in the head
.
import javax.swing.*;
class HtmlUsingBase {
public static void main(String[] args) {
final String htmlContent =
"<html>" +
"<head>" +
"<base href='http://www.gravatar.com/'>" +
"</head>" +
"<body>" +
"<h1>Image path from BASE</h1>" +
"<img src='avatar/a1ab0af4997654345d7a9" +
"49877f8037e?s=128&d=identicon&r=PG'" +
" width='128' height='128'>" +
"</body>" +
"</html>";
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel label = new JLabel(htmlContent);
JOptionPane.showMessageDialog(null, label);
}
});
}
}
Screenshot
applet vs object
Use applet
if you have found that it works more often. The object
element was introduced as a theoretical unification that would be a catch-all for any embedding of external data. It never worked well, and modern HTML development has effectively abandoned the unification idea. HTML5 introduces audio
and video
, for example and keeps img
(logical unification would surely deprecate img
, because an image can be embedded with object
).
Related Topics
How to Restart a Java Application
Converting Symbols, Accent Letters to English Alphabet
Using Heapdumponoutofmemoryerror Parameter for Heap Dump for Jboss
How to Get Java 11 Run-Time Environment Working Since There Is No More Jre 11 for Download
Call an Executable and Pass Parameters
Controlling Maven Final Name of Jar Artifact
Bounding Generics with 'Super' Keyword
What Is the Java's Internal Represention for String? Modified Utf-8? Utf-16
How to Achieve Method Chaining in Java
Java: Check the Date Format of Current String Is According to Required Format or Not
Natural Sort Order String Comparison in Java - Is One Built In
Convert an Array of Primitive Longs into a List of Longs
Java, Calculate the Number of Days Between Two Dates
Java Wait and Notify: Illegalmonitorstateexception