Spring Boot Fat Jar无法读取根目录下的application.properties配置文件
Hey there, let's break down this TypeMismatchException you're facing when starting your Spring Boot fat JAR with an external config file. This error pops up when Spring can't convert a string value from your application.properties into the required integer type for one of your app's configuration properties. Here's how to fix it:
Step 1: Identify the problematic property
First, look closely at the full exception stack trace—Spring will explicitly name the property causing the issue. You'll see a line like:
Failed to convert property value of type 'java.lang.String' to required type 'int' for property
your.app.property.name;
That your.app.property.name is the key you need to focus on.
Step 2: Fix the property value in your external application.properties
Once you've found the property, check its value for these common issues:
- Non-numeric characters: The value might have letters, quotes, or symbols (e.g.,
server.port=808oinstead of8080, ormyapp.max-connections="50"with quotes). Remove any non-digit characters so the value is a pure integer (e.g.,server.port=8080,myapp.max-connections=50). - Whitespace or full-width digits: Accidental spaces (e.g.,
myapp.timeout= 300) or full-width numeric characters (e.g.,100instead of100) will also break conversion. Trim spaces and use standard half-width digits. - Empty or missing value: If the property is left blank (e.g.,
myapp.retries=), Spring will try to convert an empty string to int—fill in a valid numeric value.
Step 3: Verify your external config is being loaded
It's possible Spring is still using the default config inside the JAR instead of your external file. To confirm:
- Start your app with the
--debugflag:java -jar $jarFileName --debug - Search the logs for a line like:
Loading properties file from file [/path/to/your/application.properties]
If you don't see this line, double-check that your external application.properties is in the exact same directory as the JAR, and that you're running the java -jar command from that directory.
Step 4: Check config file encoding
Occasionally, encoding issues (like UTF-8 with BOM) can add hidden characters to your property values. Open your application.properties in a text editor like VS Code, ensure it's saved as UTF-8 (no BOM), and re-save the file.
If you're still stuck after these steps, feel free to share the full exception stack trace and the specific property causing the issue—I can help dig deeper!
内容的提问来源于stack exchange,提问作者Oromë

