SparkJava: Image Files

and any other static files your application needs


then, this page describes a solution for you. (Note: this solution is partially documented on the SparkJava documentation but that version leaves out a few crucial details.)

  1. Make a directory src/main/resources/public/images
    • You can use the command mkdir -p src/main/resources/public/images from your top level directory.
  2. Put the image files in the directory: src/main/resources/public/images
  3. Add this line to your code: staticFiles.location(“/public”); // Static files
  4. In your HTML code:
    • if the file is /src/main/resources/public/images/cat1.jpg,
    • your img element should be: <img src="/images/cat1.jpg"> Note: do not include the /src/main/resources part in your src attribute value.
  5. You must do mvn compile each time you make changes to the contents of /src/main/resources/public,
    or for that matter, any file or directory under /src/main/resources if you want those changes to take effect, even if you make no changes to Java source code.

    The reason: the mvn compile step does more than just compile your java code; it also copies all of the contents of the src/main/resources directory into the CLASSPATH.


More on SparkJava: Image Files