Spring Boot
TemplateInputException
Error Resolving Template
Jar Execution
Java

Spring Boot gives TemplateInputException Error resolving template when running from jar

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

TemplateInputException after packaging a Spring Boot application usually means templates are not being found on the runtime classpath. The app may work in the IDE but fail from a fat jar because resource paths, packaging rules, or template resolver settings differ. Fixing this requires verifying template placement and runtime resource loading expectations.

Correct Template Location

For Thymeleaf in Spring Boot, templates should normally be under src/main/resources/templates. During packaging, these resources are copied into the jar classpath.

text
src/main/resources/templates/
  home.html
  errors/404.html

Check your controller return values match logical template names without file extensions when using default settings.

java
1import org.springframework.stereotype.Controller;
2import org.springframework.web.bind.annotation.GetMapping;
3
4@Controller
5public class HomeController {
6    @GetMapping("/")
7    public String home() {
8        return "home";
9    }
10}

Returning home maps to classpath:/templates/home.html by default.

Verify Jar Contents

If runtime fails, inspect jar contents directly to confirm templates were packaged.

bash
jar tf app.jar | grep templates

If templates are missing, build configuration may be excluding resources unintentionally. Review Maven or Gradle resource filters and include patterns.

For Maven, ensure resources are declared correctly and not overwritten by custom plugin settings.

Template Resolver Properties

Custom resolver settings can break classpath lookups when running from jar. Start with defaults unless you need custom behavior.

properties
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=true

If you point prefix to filesystem paths, it may work in local IDE runs but fail in packaged deployments where those paths do not exist.

Common Packaging Causes

Frequent jar-only failures include:

  • Using src/main/webapp assumptions in a jar deployment.
  • Returning wrong template names from controllers.
  • Case mismatch in filenames on case-sensitive environments.
  • Resource filtering or shading rules dropping templates.

Another cause is profile-specific config overriding template prefix at runtime. Check active profiles with startup logs and verify environment-specific property files.

Reproducible Diagnostic Workflow

A stable diagnosis routine:

  1. Build jar with clean command.
  2. Confirm template files are in jar archive.
  3. Run jar outside IDE.
  4. Enable debug logging for template resolution.
properties
logging.level.org.thymeleaf=DEBUG
logging.level.org.springframework.web=INFO

This captures resolver attempts and makes path mismatches obvious.

Template Engine Configuration Checks

If you override Thymeleaf beans manually, verify resolver order and character encoding explicitly. Misordered resolvers can cause template names to resolve against unexpected locations. Keep custom bean configuration minimal unless there is a clear requirement.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.thymeleaf.spring6.SpringTemplateEngine;
4import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
5
6@Configuration
7public class ThymeleafConfig {
8    @Bean
9    public ClassLoaderTemplateResolver templateResolver() {
10        ClassLoaderTemplateResolver r = new ClassLoaderTemplateResolver();
11        r.setPrefix("templates/");
12        r.setSuffix(".html");
13        r.setTemplateMode("HTML");
14        r.setCharacterEncoding("UTF-8");
15        r.setOrder(1);
16        return r;
17    }
18
19    @Bean
20    public SpringTemplateEngine templateEngine(ClassLoaderTemplateResolver resolver) {
21        SpringTemplateEngine engine = new SpringTemplateEngine();
22        engine.addTemplateResolver(resolver);
23        return engine;
24    }
25}

When custom resolver beans are unnecessary, removing them often fixes jar-only errors by restoring Spring Boot defaults.

Deployment Environment Considerations

Container images and cloud platforms may use different working directories and filesystem assumptions. Classpath templates are resilient in these environments, while absolute file paths are fragile.

If you intentionally load templates from external volumes, define that as an explicit deployment contract and verify mounts during startup checks.

For multi-module projects, ensure web module resources are packaged in the final runnable artifact. Missing module resource inclusion is a common root cause.

Common Pitfalls

A common pitfall is testing only from IDE, where resource resolution can differ from packaged jar runtime behavior.

Another issue is setting custom prefix values that rely on local filesystem paths unavailable in production containers.

Developers also return template names with incorrect case. Case-insensitive local filesystems may hide this until deployment.

Finally, build customization plugins can accidentally exclude templates directories. Always inspect jar output when resolver errors appear.

Summary

  • Put templates under src/main/resources/templates for standard jar deployments.
  • Verify packaged resources with jar tf when runtime resolution fails.
  • Prefer classpath resolver settings for portability.
  • Check profile-specific config and case-sensitive path differences.
  • Reproduce failures outside the IDE to match production behavior.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.