SparkJava:index.hbs FileNotFoundException

SparkJava: index.hbs FileNotFoundException

我使用 SparkJava 框架开发了一个网络应用程序。当我从 Eclipse 运行 时,该应用程序运行良好。当我将它打包到 jar 文件时,运行 jar 文件并在浏览器中打开网络应用程序时,我收到 500 内部错误。

控制台 returns FileNotFoundException。 hbs 文件位于 src/main/resources/templates。

org.eclipse.jetty.io.RuntimeIOException: java.io.FileNotFoundException: /templates/index.hbs
    at spark.template.handlebars.HandlebarsTemplateEngine.render(HandlebarsTemplateEngine.java:78) ~[jar:rsrc:spark-template-handlebars-2.7.1.jar!/:?]
    at App.lambda[=11=](App.java:42) ~[rsrc:./:?]
    at spark.SparkBase.handle(SparkBase.java:264) ~[jar:rsrc:spark-core-2.1.jar!/:?]
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:154) [jar:rsrc:spark-core-2.1.jar!/:?]
    at spark.webserver.JettyHandler.doHandle(JettyHandler.java:60) [jar:rsrc:spark-core-2.1.jar!/:?]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.Server.handle(Server.java:451) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266) [jar:rsrc:jetty-server-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240) [jar:rsrc:jetty-io-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596) [jar:rsrc:jetty-util-9.0.2.v20130417.jar!/:?]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.run(QueuedThreadPool.java:527) [jar:rsrc:jetty-util-9.0.2.v20130417.jar!/:?]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_181]
Caused by: java.io.FileNotFoundException: /templates/index.hbs
    at com.github.jknack.handlebars.io.URLTemplateLoader.sourceAt(URLTemplateLoader.java:70) ~[jar:rsrc:handlebars-4.0.6.jar!/:?]
    at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:357) ~[jar:rsrc:handlebars-4.0.6.jar!/:?]
    at com.github.jknack.handlebars.Handlebars.compile(Handlebars.java:343) ~[jar:rsrc:handlebars-4.0.6.jar!/:?]
    at spark.template.handlebars.HandlebarsTemplateEngine.render(HandlebarsTemplateEngine.java:75) ~[jar:rsrc:spark-template-handlebars-2.7.1.jar!/:?]

我不明白的是,为什么当我在 Eclipse 中 运行 它可以工作,但当我将它打包到 jar 时却不能。

知道问题出在哪里吗?

URLTemplateLoader.sourceAt() 抛出这个因为 getResource() return null,这里是代码:

  protected URL getResource(final String location) throws IOException {
    File file = new File(location);
    return file.exists() ? file.toURI().toURL() : null;
  }

您将相对路径传递给 File 构造函数,它是从工作目录解析的。

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

File javadoc

我自己解决了这个问题。问题出在 Eclipse 上。因为它对遇到同样问题的其他人有用,所以我会自己创建答案。

问题是由于 /src/main/resource 文件夹包含在 Java 构建路径中,但由于某种原因并未包含所有子文件夹。值“**”已分配给“Excluded:”项目。我将值更改为“(None)”后,找到了模板文件。