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.
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.
Check your controller return values match logical template names without file extensions when using default settings.
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.
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.
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/webappassumptions 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:
- Build jar with clean command.
- Confirm template files are in jar archive.
- Run jar outside IDE.
- Enable debug logging for template resolution.
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.
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/templatesfor standard jar deployments. - Verify packaged resources with
jar tfwhen 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
- Spring Boot Gradle how to build executable jar
- Spring Boot /h2-console throws 403 with Spring Security 1.5.2
- Spring Boot Handler dispatch failed; nested exception is java.lang.NoSuchMethodError
- Spring Boot Hibernate and Flyway boot order
- Spring boot hikari - dataSource or dataSourceClassName or jdbcUrl is required issue
- Spring Boot hotswap with IntelliJ IDE
- Spring Boot How do you specify an environment variable that has dashes in the application.properties?
- Spring Boot How to add another WAR files to the embedded tomcat?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.