【问题标题】:Embed HTML in JAR with Eclipse使用 Eclipse 在 JAR 中嵌入 HTML
【发布时间】:2015-02-03 11:29:48
【问题描述】:

我已经阅读了this 的问题,但仍然没有运气。

我想在 HTML 页面中嵌入 CSS 文件。在我的项目中我有res 文件夹,在.classpath 我有<classpathentry kind="src" path="res"/> 以便嵌入我的文件。

|-- src
|   `-- com
|       `-- pkg
|           `-- blahblahblah
|               `-- CTLiner.java
`-- res
    `-- html
        |-- index.html
        `-- style.css

CTLiner.java 是主类文件。

URL u = CTLiner.class.getResource("/html/index.html");
System.out.println(u.toURI().toString());
webView.showPage(u);

我使用此代码加载index.html。如果我从 Eclipse 运行它,我会得到以下输出

file:/home/user/workspace/CTLiner/bin/html/index.html

<link href="style.css" rel="stylesheet" type="text/css" />

效果很好。当我将项目导出为 Runnable JAR 时,代码输出: rsrc:html/index.html 并且没有找到 CSS 文件。

我相信如果我将 index.htmlstyle.css 文件向上移动(我可以这样说吗?)一切都会正常工作,但是还有其他解决方案吗?

【问题讨论】:

  • 试试放到src/main/resources下?
  • 如果我理解你的意思,@TungstenX 的工作原理是一样的。
  • 我发现把它放在你的 IDE/编译器想要它的地方比使用它更容易;) 你看过 JAR 文件的内容了吗? (使用winrar或winzip等)
  • 是的。如果我按照问题中提到的方式执行所有操作,html 文件夹将出现在 JAR 的根目录中。我不知道如何检查它在哪里搜索style.css。等一下。我知道,我会检查的。
  • @TungstenX 它在 JAR 的根目录中查找。

标签: java eclipse embedded-resource


【解决方案1】:

我使用的是Package required libraries into generated JAR,但我必须使用Extract required libraries into generated JAR。我还不知道为什么。

当我使用Package方法时,java习惯在JAR文件的根目录中寻找style.css,不管HTML文件在哪里。

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 2011-11-23
    • 1970-01-01
    • 2012-10-04
    • 2015-08-20
    • 2016-02-08
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    相关资源
    最近更新 更多