0

I have a feeling I'm missing something critical when it comes to bundling resources in JAR files using Maven. My directory structure is the standard Maven one.

When using the IDE I reference the resource files as \main\resources\public\images\path and it works fine, but when Maven css js and images dont work and the site looks bad. If I look in the jar I find all the css, js and images. When I try to open the css link in a browser I get a 404 error. Here is my pom.xml https://shrib.com/#XGISjWGN7QRfa2EIZD4r. In eclipse it works fine. I use Spark.staticFiles.location("public"); to load resources.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
  • Please, show us some code so that we can help you better. This is confusing and doesn't make a lot of sense, so please try to help us help you by giving us some more information. Paste your pom file in your question, between triple backticks (```). Show us some of the code you're working with, and your html. – Software Engineer Nov 05 '19 at 12:09
  • Jar files are for java and are put on your classpath and referenced from there -- you should never reference `main/resources` in your code. Maven will package the contents of the resources directory into the root of the jar. HTML, CSS, and Javascript aren't usually packaged in a jar -- they are usually kept separate and deployed to a file system on an http server. Though it is possible to package them in a jar, it's not really a great way to go about web content development. – Software Engineer Nov 05 '19 at 12:16
  • thank you for your resopnse . this image show the jar file opened on winrar https://ibb.co/8PvfYf1 – Trafalgar Law Nov 05 '19 at 12:27

1 Answers1

0

I can't comment for Spark, however when you create an artifact (jar, fat or not), it merely doesn't contain main/resources folder (open the jar with tool like winrar and check yourself).

Another difficulty is that if you use something that works with File abstraction - its probably wrong inside the artifact. File describes a place on a filesystem, and you're trying to read something from the artifact.

Usually you can use inside the code that reads the resource:

getClass().getResourceAsStream("/public/impages/path/sample.js") 
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • thanks for your response .in my code i use `Spark.staticFiles.location("public");` and it work perfectly . and in the jar file when i open it with winrar the folder public exists with all css js and images but not work . if i open path of one of the resources i got 404 not found . – Trafalgar Law Nov 05 '19 at 11:19