Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How springboot runs the jar package to read external configuration files

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how springboot runs jar package to read external configuration files". In daily operation, I believe many people have doubts about how springboot runs jar package to read external configuration files. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how springboot runs jar package to read external configuration files". Next, please follow the editor to study!

Method 1: relative path setting profile

(1) create a configuration file conf.properties in the same level directory of the jar package and write the configuration data:

ConfData=data

(2) start writing automated test code

/ / from www.fhadmin.cnpublic class Test {public String getData () throws IOException {/ / read configuration file Properties properties = new Properties (); File file = new File ("conf.properties"); FileInputStream fis = new FileInputStream (file); properties.load (fis); fis.close (); / / get profile data String confData = properties.getProperty ("confData") System.out.println (confData);}}

(3) execute jar package

Java-jar jarNanexxx

Method 2: absolute path setting profile

To solve the problem: using the relative path method to manually execute the jar package in the same level directory of the jar package is no problem, but using the crontab file of the linux system to schedule the time is wrong, because when we execute a script manually, it is carried out in the current shell environment, and the program can find the environment variables; while the system automatically performs task scheduling, it will not load any other environment variables except the default environment. Therefore, you need to specify all the environment variables needed for the task to run in the crontab file, or use the absolute path in the program.

(1) create a configuration file conf.properties in the same level directory of the jar package and write the configuration data:

ConfData=data

(2) start writing automated test code

/ / from www.fhadmin.cnpublic class Test {public String getData () throws IOException {/ / get the jar package sibling directory String path = this.getClass (). GetProtectionDomain (). GetCodeSource (). GetLocation (). GetPath (); String [] pathSplit = path.split ("/"); String jarName = pathSplit [pathSplit.length-1]; String jarPath = path.replace (jarName, "); String pathName=jarPath+" minhang.properties " System.out.println ("profile path:" + jarPath); / / read configuration file Properties properties = new Properties (); File file = new File (pathName); FileInputStream fis = new FileInputStream (file); properties.load (fis); fis.close (); / / get profile data String confData = properties.getProperty ("confData") System.out.println (confData);}}

(3) execute jar package

Java-jar jarNanexxx

At this point, the study on "how springboot runs the jar package to read external configuration files" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report