In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how java maven projects read configuration file information. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Maven project read profile information
Directory structure
Main class
App.java
Package com.tomy.hive; import java.io.*;import java.util.Properties; / * Hello world! * * / public class App {private static String JDBC_URL; private static String JDBC_DRIVER; / * read configuration file * @ return * / public static void readConfigFile (String cfgFile) {try {InputStream in = App.class.getClassLoader () .getResource (cfgFile) .openStream () Properties prop = new Properties (); prop.load (in); JDBC_URL = prop.getProperty ("jdbc.url"); JDBC_DRIVER = prop.getProperty ("jdbc.driver");} catch (IOException e) {e.printStackTrace ();}} public static void main (String [] args) {readConfigFile ("resources/jdbc.properties") System.out.println (JDBC_URL); System.out.println (JDBC_DRIVER);}} configuration file
Jdbc.properties
Jdbc.url=jdbc:mysql://10.6.52.35:3306/test?characterEncoding=utf-8&serverTimezone=UTC&useSSL=falsejdbc.driver=com.mysql.jdbc.Driverpom file 4.0.0 com.tomy.hive hive-demo 1.0-SNAPSHOT hive-demo http://www.example.com UTF-8 1.81.8 junit junit 4.11 test Org.apache.maven.plugins maven-surefire-plugin true org.apache.maven.plugins maven-jar-plugin 3.0.2 true com. Tomy.hive.App maven-resources-plugin copy-resources validate copy-resources ${project.build.directory} / Conf src/main/resources true src/main/resources * * / * * .properties / resources console runs
The jar command runs
Maven project reads the correct posture of the resources configuration file
The structure of our maven project is as follows:
Use relative paths to read resource files in the resources directory
InputStream in = new InputStream (new File ("src/main/resources/car.txt"))
In this way, when running locally, it can be read normally and will not report an error, but if it is typed into a jar package, the path error will be reported at run time.
As you can see from the structure of the jar package, the location of the resource files in the resources directory has changed, at the outermost layer of the project, resulting in a change in the relative path.
At this point, we can get the correct configuration file path through the getClassLoader () method.
(this can also be changed to the name of the class)
InputStream in = this.class.getClassLoader () .getResourceAsStream (path)
In this way, the configuration file can be read normally either locally or by running the jar package.
Thank you for reading! This is the end of this article on "how to read configuration file information for java maven projects". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.