HTML from a Jar file that links resources (such as CSS or images) using relative links will work fine.
eg.
This example loads the HTML (which has a relative link to the image) from the 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
Downloadable HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <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>
eg. 2
For dynamically generated HTML, the JRE is likely to use the location of the class file as the intended HTML location. But, to eliminate all doubts, we can specify the base element in 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

source share