Spark: Could not load static resources from jar files

Created on 30 Apr 2016  路  18Comments  路  Source: perwendel/spark

I can package all my static resources into a jar file, when I used version 2.3. But after I update my spark to the latest version of master branch, it does not work.

2016-04-30 09:54:02,147 [main] ERROR spark.staticfiles.StaticFilesConfiguration[116] - Error when creating StaticResourceHandler
java.io.FileNotFoundException: class path resource [public] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/home/towerhe/Repository/Codes/maoguang/java/mg-vdra/standalone/target/mg-vdra-standalone-0.1.0-SNAPSHOT/lib/mg-vdra-core-0.1.0-SNAPSHOT.jar!/public
    at spark.utils.ResourceUtils.getFile(ResourceUtils.java:213) ~[spark-core-2.4-SNAPSHOT.jar:na]
    at spark.resource.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:46) ~[spark-core-2.4-SNAPSHOT.jar:na]
    at spark.staticfiles.StaticFilesConfiguration.configure(StaticFilesConfiguration.java:105) ~[spark-core-2.4-SNAPSHOT.jar:na]
    at spark.Service.staticFileLocation(Service.java:208) [spark-core-2.4-SNAPSHOT.jar:na]
    at spark.Spark.staticFileLocation(Spark.java:977) [spark-core-2.4-SNAPSHOT.jar:na]
    at org.maoguang.vdra.Application.init(Application.java:26) [mg-vdra-core-0.1.0-SNAPSHOT.jar:na]
    at org.maoguang.vdra.Main.run(Main.java:31) [mg-vdra-core-0.1.0-SNAPSHOT.jar:na]
    at org.maoguang.vdra.Main.main(Main.java:54) [mg-vdra-core-0.1.0-SNAPSHOT.jar:na]
Bug Please prioritize

Most helpful comment

A remark here that could save some time for future people still running in Error when creating StaticResourceHandler with spark > 2.5 : the folder invocation must start with a "/" so:
Spark.staticFileLocation("/public"); works both in IntelliJ and in a jar while
Spark.staticFileLocation("public"); works only in IntelliJ

All 18 comments

Do you know if this is just in the latest version or could it be any commit between 2.3 release and latest?

Bug seems to come from efa37625d75b2281f1dabbd2e20a7ca2716adb2b (the static file rewrite from last year).

@towerhe
Can you please describe your "setup". E.g. are you using Spark with embedded Jetty or are you deploying on another web server?

To reproduce:

@tipsy tx

Sorry for my later reply. I'm using Spark with embedded Jetty.

I can reproduce the bug with @tipsy steps. It works when running from within Intellij. Strange!

I've identified the problem. When the static file location is added this is done via a ClassPathResource handler (a copy of a jetty class). The problem is that this just works for files in the file system and not files in a jar.

I don't have a solution yet. It might involve this class: http://docs.spring.io/autorepo/docs/spring-framework/2.5.x/api/org/springframework/core/io/InputStreamResource.html

If someone has experience of similar problems I'd be grateful for any help.

I had the same problem and did a patch to support jar-embedded static directory. However my understanding is that it involves removing support for welcome file (index.html) when accessing a static folder (afaik there is no easy way to tell a classpath resource is a directory or a file).

I've implemented a solution for this #537 that made the @tipsy static-file-bug project work.
Please try it out.

Live as 2.5 now!

Well it didn't solve my issue but it wasn't working in 2.3 so I guess it's not a big deal right now.

I want to read static resources which reside in a different jar in the classpath. I don't know if you'd find it useful but if you do I'll open another issue and start working on a PR to implement it properly.

After a few tests that actually works.

My problem is a bit more complicated. It involves mixing static resources from different classpath sources. You can have multiple jars in classpath containing static resources but you can't mix physical classpath folders and jars in classpath.
You'll probably won't find it very useful. I need it so I will adapt my patch to the new 2.5 source.

Feel free to make a new issue/PR. If it doesn't change current functionality and doesn't add too much code, I see no harm in merging it.

Thanks, I'll look into it. FYI I've also created a small maven project to illustrate (and easily test) the issue in https://github.com/MouettE-SC/static-files-jar , should you feel curious.

A remark here that could save some time for future people still running in Error when creating StaticResourceHandler with spark > 2.5 : the folder invocation must start with a "/" so:
Spark.staticFileLocation("/public"); works both in IntelliJ and in a jar while
Spark.staticFileLocation("public"); works only in IntelliJ

Hi guys - I was able to reproduce this bug when the static resources folder is set to "/". Works fine when running running from intelliJ but not when doing mvn clean install then java -jar filename.jar

So it looks like the static resources folder must be "/templates" or something. Interestingly it will not work when I use "/public" in the resources folder.

Just thought I would let you know :). I am using Spark 2.5

I am running into a similar issue as well, even though I am running 2.5 and am using "/static" as my static folder. Works fine in IDE but not as a jar.
I traced through it and it throw an IOException in StaticFilesConfiguration at this line:
if (resource.getURL().getProtocol().equals("jar")) {

Any other idea how to get around this?

I'm still seeing this issue in v2.6.0. I'm compiling to a JAR, with my static files in public/. When I try to get http://<host>:<port>/firstDirectory, I get this exception, but getting http://<host>:<port>/firstDirectory/ works fine.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anxiaoyi picture anxiaoyi  路  3Comments

dmacvicar picture dmacvicar  路  5Comments

wowselim picture wowselim  路  5Comments

skyghis picture skyghis  路  3Comments

chibongho picture chibongho  路  4Comments