You cannot read a File from inside a JAR. This fails due to the fact that the File has to point to an actual file resource on the file system and not something inside a JAR.

Let Spring do the heavy lifting and use the Resource abstraction to hide the nasty internals. So instead of using a String use a Resource and prefix the value of the property with classpath: to make sure it is loaded from the classpath. Then use an InputStreamReader instead of FileReader to obtain the information you need.

@Value("${pont.email.template.location}") 
private Resource templateLocation;
----------------
BufferedReader reader = new BufferedReader(new InputStreamReader(templateLocation.getInputStream()));

In your application.properties prefix with classpath:.

pont.email.template.location=classpath:templates/mailTemplate.html

Now it should work regardless of the environment you are running in.

Answer from M. Deinum on Stack Overflow
Top answer
1 of 3
16

I'm using spring boot to build a upload sample, and meet the same problem, I only want to get the project root path. (e.g. /sring-boot-upload)

I find out that below code works:

upload.dir.location=${user.dir}\\uploadFolder
2 of 3
8

@membersound answer is just breaking up the hardcoded path in 2 parts, not dynamically resolving the property. I can tell you how to achieve what you're looking for, but you need to understand is that there is NO project.basedir when you're running the application as a jar or war. Outside the local workspace, the source code structure doesn't exist.

If you still want to do this for testing, that's feasible and what you need is to manipulate the PropertySources. Your simplest option is as follows:

Define an ApplicationContextInitializer, and set the property there. Something like the following:

    public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext appCtx) {
        try {
            // should be /<path-to-projectBasedir>/build/classes/main/
            File pwd = new File(getClass().getResource("/").toURI());
            String projectDir = pwd.getParentFile().getParentFile().getParent();
            String conf = new File(projectDir, "db/init").getAbsolutePath();
            Map<String, Object> props = new HashMap<>();
            props.put("spring.datasource.url", conf);
            MapPropertySource mapPropertySource = new MapPropertySource("db-props", props);
            appCtx.getEnvironment().getPropertySources().addFirst(mapPropertySource);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }}

Looks like you're using Boot, so you can just declare context.initializer.classes=com.example.MyApplicationContextInitializer in your application.properties and Boot will run this class at startup.

Words of caution again:

  1. This will not work outside the local workspace as it depends on the source code structure.

  2. I've assumed a Gradle project structure here /build/classes/main. If necessary, adjust according to your build tool.

  3. If MyApplicationContextInitializer is in the src/test/java, pwd will be <projectBasedir>/build/classes/test/, not <projectBasedir>/build/classes/main/.

🌐
Java Training School
javatrainingschool.com › home › spring boot application properties file
Spring boot application properties file - Java Training School
April 26, 2022 - Spring Boot comes with a built-in mechanism for application configuration using a file called application.properties. It is located under the src/main/resources folder, as shown below. This file is like a properties file which stores key-value pair.
🌐
Spring
docs.spring.io › spring-boot › reference › features › external-config.html
Externalized Configuration :: Spring Boot
You can provide default values for your application in application.properties (or whatever other basename you choose with spring.config.name) in one of the default locations. These default values can then be overridden at runtime with a different file located in one of the custom locations. By default, when a specified config data location does not exist, Spring Boot will throw a ConfigDataLocationNotFoundException and your application will not start.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 737994 › springboot-application-path
SpringBoot application path. - Microsoft Q&A
February 19, 2022 - The same case is with application.properties. But if i put this files in the top of the directory tree (/) then the springboot application reads them.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › spring_boot › spring_boot_application_properties.htm
Spring Boot - Application Properties
Spring Boot supports YAML based properties configurations to run the application. Instead of application.properties, we can use application.yml file. This YAML file also should be kept inside the classpath. The sample application.yml file is given below − · spring: application: name: demoservice server: port: 9090 · Instead of keeping the properties file under classpath, we can keep the properties in different location or path.
🌐
Medium
aliwertt.medium.com › understanding-application-properties-in-spring-boot-b68fcd6a9313
Understanding application.properties in Spring Boot | by Aliwert | Medium
January 15, 2025 - In a Spring Boot application, the application.properties file is a key configuration file that helps to customize various aspects of the application. It is typically located in the src/main/resources directory of your project.
🌐
JavaTechOnline
javatechonline.com › home › how to write spring boot application properties files
How To Write Spring Boot Application Properties Files - JavaTechOnline
November 15, 2024 - Unlike the standard Spring framework ... Therefore, we don’t need to specifically register a PropertySource, or even provide a path to a property file....
🌐
Baeldung
baeldung.com › home › spring › spring boot › properties with spring and spring boot
Properties with Spring and Spring Boot | Baeldung
July 25, 2025 - This means that we can simply put an application.properties file in our src/main/resources directory, and it will be auto-detected. We can then inject any loaded properties from it as normal.
🌐
Quora
quora.com › How-do-you-put-a-properties-file-from-a-classpath-in-Spring-Boot
How to put a properties file from a classpath in Spring Boot - Quora
Answer: Well For starters we try to put properties in seperate file under src/main/resource/ application- .yml file and then you can load the specific properties file by passing the [code]-Denvname=local [/code]when running the project to load this configurations . Now to access these conf...
🌐
Spring
docs.spring.io › spring-boot › docs › 1.0.1.RELEASE › reference › html › boot-features-external-config.html
21. Externalized Configuration
Spring Boot uses some relaxed rules for binding Environment properties to @ConfigurationProperties beans, so there doesn’t need to be an exact match between the Environment property name and the bean property name. Common examples where this is useful include underscore separated (e.g. context_path binds to contextPath), and capitalized (e.g. PORT binds to port) environment properties. Spring will attempt to coerce the external application properties to the right type when it binds to the @ConfigurationProperties beans.
🌐
Spring
docs.spring.io › spring-boot › how-to › properties-and-configuration.html
Properties and Configuration :: Spring Boot
By default, properties from different sources are added to the Spring Environment in a defined order (see Externalized Configuration in the “Spring Boot Features” section for the exact order). You can also provide the following System properties (or environment variables) to change the behavior: spring.config.name (SPRING_CONFIG_NAME): Defaults to application as the root of the file name.
🌐
GitHub
github.com › spring-projects › spring-framework › issues › 23544
Cannot use relative path following placeholder in @TestPropertySource locations · Issue #23544 · spring-projects/spring-framework
August 29, 2019 - When I remove this ".." characters (so i've got file:${user.dir}/config/application.properties), then it gives me this path: /home/pawel/projekty/simulatorserver/server/config/application.properties, but I want to get to this path: /home/pawel/projekty/simulatorserver/config/application.properties (without server directory). I did some investigation and I found out that org.springframework.util.StringUtils#cleanPath(String path) removes ${user.dir} from path.
Author   spring-projects
🌐
HowToDoInJava
howtodoinjava.com › home › spring boot › different ways to load property files in spring boot
Different Ways to Load Property Files in Spring Boot
April 3, 2024 - In this example, Spring Boot loads the application.properties file from the specified external location (/path/to/external/application.properties) instead of the default location.