簡體   English   中英

是否可以/如何在JAR中嵌入和訪問HTML文件?

[英]Is it possible/how to embed and access HTML Files in a JAR?

我現在有點受阻:我寫了一個非常復雜的Java桌面應用程序(沒有Applet / Web App!),它有自己的“用戶手冊”。 本手冊包含一些HTML和JPG文件。 本手冊使用JEditorPane顯示在我的應用程序的“幫助菜單”中。

到現在為止還挺好。 只要我使用Eclipse啟動Programm,這種方法就可以正常工作。 只要我將部署版本創建為可運行的jar(使用launch4j將其包裝到.exe中),HTML“Viewer”就無法顯示用戶手冊(圖像丟失)。

我明白為什么會發生這種情況,但我不知道如何解決/規避這個問題。

我的應用程序通過getClass()。getResource()加載其資源(屬性文件,圖標等)。 例子:

this.setIconImage(new ImageIcon(getClass().getResource("/images/dialog-question.png")).getImage());

stream = new BufferedInputStream(MABIUpdater.class.getResourceAsStream("/settings.properties"));

就像我之前說的那樣,這確實很有效(從Eclipse中啟動App或者作為包裝的可執行文件或runnable-jar啟動。

所以我試圖像這樣訪問我的HTML“手冊”:

File manual = new File(getClass().getResource("/manual/help.html").toURI());

jEditorPane.setPage(manual.toURI().toURL());

這不起作用。 通過Eclipse啟動程序我看到手冊但缺少圖像。 通過jar / exe啟動它我得到一個空框架。

那么如何實現這一目標還有什么“訣竅”呢? 我猜一個問題是HTML頁面本身,因為它無法訪問該jar中的鏈接圖像。 這是一個非常小的HTML文件示例(無法使用圖像):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html lang="de">
    <head>
        <title>Manual</title>
    </head>
    <body>
        <h1>Example: </h1>
        <p>fubar</p>
        <img style="display: block; text-align: center;" src="../manual/img/Shot01.png" width="666" height="644" border="0" alt="Bildtext">
        <p><a href=\"http://www.google.com/\">blablubb</a></p>
    </body>
</html>

我希望我的問題很明確,有人有想法;)。

編輯:所有必需的HTML文件和圖像都在JAR文件/類路徑中。 (只是為了讓這個更清楚)

File manual = new File(getClass().getResource("/manual/help.html").toURI());

這就是它出錯的地方。 Java無法從創建File對象

將其保留為URL並將其用於setPage(..)


至於更普遍的問題。

來自Jar文件的HTML通過相對引用鏈接資源(例如CSS或圖像)將正常工作。

例如

此示例從Jar加載HTML(具有對圖像的相對引用)。

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();
                }
            }
        });
    }
}

截圖

JEdi​​torPane顯示Jar'd HTML

HTML

正在加載的HTML。

<!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>

EG 2

對於動態創建的HTML,JRE 可能會使用類文件的位置作為HTML的假定位置。 但是為了消除所有疑問,我們可以在head指定base元素。

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);
            }
        });
    }
}

截圖

在此輸入圖像描述

獲得URI后,您將其轉換為文件,然后返回URI,然后轉換為URL。 只需將URI轉換為URL即可獲得更好的成功

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM