0

How/Where can I set the relative path location for a packaged (jar) Spring Boot jar application?

The following is what works in my IDE (IntelliJ).

I have in my application.properties file the following properties.

converter.output=upload-dir/output/ converter.input=upload-dir 

I have a Java class that controls the properties for me.

@Component @ConfigurationProperties("converter") public class ConverterProperties { //getters //setters } 

I have the following directory structure within the IDE.

src/ target/ upload-dir/ upload-dir/output/ pom.xml README.txt 

However, I am wanting to know where my upload-dir and upload-dir/output folders would be when I generate a jar and run it from a folder? I have tried putting the folder in the same location as the jar

C:\app\app.jar C:\app\upload-dir\ C:\app\upload-dir\output\ 

But no dice. I setup the @ConfigurationProperties based on this documentation. https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html, but I can't seem to find anywhere in there were it talks about packaged jar relative paths.

1 Answer 1

2

A typical spring boot application displays some of the information you are looking for in the first line at info level (Starting Application {name} on {host} with PID 1234 ({jarpath} started by {user} in {workdir})

Looking at the source code in StartupInfoLogger, it looks like you need to use the ApplicationHome helper class (in package org.springframework.boot) in order to get that absolute path of the JAR file of your running spring boot application.

Here is an example of Java code to retrieve the location of the jar file and the directory containing the jar file. This is then used to create the uploadDir file (assuming it is a subdirectory of the jar directory)

ApplicationHome home = new ApplicationHome(this.getClass()); File jarFile = home.getSource(); File jarDir = home.getDir(); File uploadDir = new File(jarDir, "upload-dir"); 

You would want to run this from within one of your application classes running in the spring boot app. Looks like it uses the class passed to the constructor of ApplicationHome in order to find the jar which contains that class.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That got me going in the right direction and to the final solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.