可运行的 Jar 文件找不到捆绑的资源
我有一个简单的 Web 服务器(在 Vertx 中实现),它提供由 HTML、CSS 和 Javascript 文件组成的静态内容。我在最新的 Eclipse IDE 中开发了这个服务器。
服务器在开发环境中运行良好,但是当我生成可运行的Jar文件时,该jar文件无法“找到” 网页。
我使用的 Vertx Web 代码非常简单。我正在使用 Vertx Web 文档中提供的静态内容方法:
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route(HttpMethod.GET,"/*").handler(StaticHandler.create(".").setIndexPage("index.html"));
server.requestHandler(router);
server.listen(8099,result->{
if(result.succeeded())
log.info("Main Server has started! Listening on Port "+server.actualPort());
else if(result.failed())
log.error("Main Server start FAILED. Reason: "+result.cause());
});
我的项目的目录结构如下:
testmain
|
--src
| |
| --<package net.test.app>
| |
| --TestServer.java
|
--resources
|
--index.html
|
-- sheet.css
|
--script.js
我使用常用方法生成可运行 Jar:导出 -> 可运行 Jar 文件。在导出对话框中,我指定 TestServer 应用程序和目标 Jar,然后选择“将所需的库提取到生成的 jar 中”。
使用 7-Zip 检查可运行 Jar 文件的内容,我发现所有必需的类文件、库和静态资源都在 Jar 文件中。不幸的是,当我运行该应用程序时,找不到这些文件。让应用程序找到它们的唯一方法是将它们复制到与 jar 文件相同的文件夹中/即使这样,也找不到 Javascript 文件。
我确信有一种方法可以配置可运行 Jar,以便应用程序在 jar 文件中查找其资源,因为其他应用程序(基于 servlet 或 Spring Boot 应用程序)经过打包,以便它们的可运行文件是独立的。不幸的是,Eclipse 文档和任何描述可运行 Jars 的页面都没有解决这个问题。
那些讨论在可运行对象中使用资源的人建议更改访问文件的代码,以便在类路径上找到它们。这很好,但因为我使用 Vertx web 访问静态文件,所以无法重写读取它们的代码。
有没有办法配置我的可运行 Jar,以便它在 jar 中查找其资源?或者如果失败了,有没有办法让 Vertx 从 Jar 内访问它们?
I have a simple web server (implemented in Vertx) which serves static content composed of HTML, CSS, and Javascript files. I have developed this server in the latest Eclipse IDE.
The server works well in the development environment, but when I generate a runnable Jar file, the jar file fails to "find" the
web pages.
The Vertx Web code that I am using is pretty straightforward. I am using the static content method as provided on the Vertx Web documentation:
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route(HttpMethod.GET,"/*").handler(StaticHandler.create(".").setIndexPage("index.html"));
server.requestHandler(router);
server.listen(8099,result->{
if(result.succeeded())
log.info("Main Server has started! Listening on Port "+server.actualPort());
else if(result.failed())
log.error("Main Server start FAILED. Reason: "+result.cause());
});
The directory structure of my project is below:
testmain
|
--src
| |
| --<package net.test.app>
| |
| --TestServer.java
|
--resources
|
--index.html
|
-- sheet.css
|
--script.js
I generate the runnable Jar using the usual method: Export->Runnable Jar File. In the export dialog, I specify the TestServer application and destination Jar, and I select "Extract required libraries into generated jar".
Examining the contents of the runnable Jar file using 7-Zip, I see that all necessary class files, libraries, and static resources are in the Jar file. Unfortunately, when I run the application, the files cannot be found. The only way to make the application find them is to copy them into the same folder as the jar file/ Even then, the Javascript file isn't being found.
I am sure that there is a way to configure the runnable Jar so that the application will look within the jar file for its resources, because other applications (servlet- based or Spring Boot applications) are packaged so that their runnables are self- contained. Unfortunately, neither the Eclipse documentation nor any of the pages describing runnable Jars address this issue.
Those that talk about using resources within a runnable suggest changing the code that accesses the files so that it finds them on the classpath. This is good, but because I am using Vertx web to access the static files, rewriting the code for reading them is not an option.
Is there a way to configure my runnable Jar so that it looks within the jar for its resources? Or failing that, is there a way to make Vertx access them from within the Jar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论