Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 16:56:05 09月24日 Update

This article is about how to get the directory path where the jar package is located after the release of the SpringBoot project jar, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article, without saying much, let's take a look at it.

SpringBoot project jar release gets the path to the directory where the jar package is located: ApplicationHome ah = new ApplicationHome (getClass ()); File file = ah.getSource (); System.out.println (file.getParentFile (). ToString ())

Description:

This method obtains the benefit of the path where the jar package is located, and will accurately obtain the path of the jar based on whether it is currently in the local test environment or published to the production environment.

SpringBoot executable jar runtime output file path problem

Under the environment of Ubuntu system, the project runs the project generation file in the form of jar package and gets the path setting of the file.

1. Use the following methods in the previous project to get the directory when typing the war package and running the project locally.

What is obtained in IDEA is: / home/xxx/xxx/ (projectName) / target/classes

If you use what you get in the jar package: / home/xxx/BOOT-INF/classes

String path = (String.valueOf (Thread.currentThread (). GetContextClassLoader (). GetResource (") .replaceAll (" file:/ ",") .replaceAll ("% 20", ") .trim ()

two。 When the project runs with java-jar xxx.jar in 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"); to get the path where the current jar is running (the same directory as the jar file), and use that path as the root directory to store the files generated in the project.

The above is how to get the directory path where the jar package is located after the release of the SpringBoot project jar. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Path project file location run directory environment method more knowledge article problem generation practical predecessor peer benefit that is work meeting engineering Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi Shulou Technology Microsoft NVidia