How to get the directory path of jar package after SpringBoot project jar is released

  • 2021-12-11 07:34:37
  • OfStack

Directory SpringBoot project jar release obtains the directory path where jar package is located SpringBoot executable jar runtime output file path problem

SpringBoot project jar release obtains the directory path where jar package is located


ApplicationHome ah = new ApplicationHome(getClass());
File file = ah.getSource();
System.out.println(file.getParentFile().toString());

Description:

This approach captures the benefits of the path where the jar package resides, and accurately captures the path where the jar resides, depending on whether it is currently a local test environment or a release to a production environment.

SpringBoot executable jar runtime output file path problem

Under the Ubuntu system environment, the project runs the project generated file in the form of jar package and the path setting to obtain the file

1. Use the following methods in previous projects to get directories when typing war packages and running projects locally.

Obtained in IDEA is:/home/xxx/xxx/(projectName)/target/classes;

If you use:/home/xxx/BOOT-INF/classes that is obtained in the jar package


String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))).replaceAll("file:/", "").replaceAll("%20", " ").trim();

2. When the project runs with java-jar xxx. jar for the jar package, jar runs as a separate file and the file is not generated in the jar package.

So use System. getProperty ("user. dir"); Gets the path where the current jar is running (and the sibling directory where the jar files are located), and uses this path as the root directory to store the files generated in the project.


Related articles: