Manually launched Spring Boot application from .jar not working properly

10,840

Solution 1

A normal Spring Boot application built from http://start.spring.io would show the templates folder within the src/main/resources. This is because by default the contents of that folder are made available to the classpath and packaged at the root of the jar file. I am guessing you modified the project in IntelliJ to also pick up the src/main/webapp directory. Unfortunately that doesn't work when it comes to packaging the jar. You would need to work with the maven resources plugin to specify the additional directory to be included in the jar file.

My recommendation is to go to start.spring.io. Select Web and Thymeleaf as dependencies and then download the zip file. When you look in that file you will see the default structure along with settings for any plugins to make it work. Mirror that structure within you application.

If you don't have the ability to change the project structure then check out the maven-resources-plugin for specifics on how to add those directories.

Solution 2

You can see what exactly IntelliJ is doing, by looking at first line in its console after running app. There should be full command called by IntelliJ.

And how .jar is build? Maven/Gradle? maybe there's some problem with pom/build script

It should be comment, but my reputation is too low

Share:
10,840
Vlad  Sereda
Author by

Vlad Sereda

Updated on June 05, 2022

Comments

  • Vlad  Sereda
    Vlad Sereda almost 2 years

    The problem is when I run .jar file(Spring Boot MVC app) from Intellij IDEA, application works fine, but when I try to run same file from the command line, template resolver cannot find template(.html) files. And this seems to be reasonable since when I opened .jar with file archiver I did not find any traces of that files.

    How Intellij make it work proper? And how can I do the same? My project structure: enter image description here

    here they are, in '/templates' folder

    .jar file opened in archiver:

    enter image description here

    As I said, there is no .html files, so how Intellij add them when running .jar?

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.vlad.pet.contactlist</groupId>
      <artifactId>web-app</artifactId>
      <version>1.0-ALPHA</version>
      <packaging>jar</packaging>
    
      <name>web-app</name>
      <url>http://maven.apache.org</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
      </properties>
    
    
      <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.6.RELEASE</version>
      </parent>
    
      <dependencies>
        <dependency>
          <groupId>com.vlad.pet.contactlist</groupId>
          <artifactId>model</artifactId>
          <version>1.1-inmemory</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
          <groupId>org.thymeleaf.extras</groupId>
          <artifactId>thymeleaf-extras-springsecurity4</artifactId>
          <version>2.1.2.RELEASE</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
    </project>