Java reads configuration files from other jar packages

Source: Internet
Author: User

Recently, to do something, you need to control the database of things, Project packaging framework, it is not to mention the interest to read,

You want to read the configuration file of the data connection in the bottom implementation of the framework (for example: Url,port,username,password, etc.),

Use JDBC to create a data connection, and when you get the connection, things are done.

Found in a A.jar package inside the root directory, there is a configuration file b.properties, which has the required configuration.

The difficulty comes, the properties can load src below the properties file, direct prop.load (This.getclass (). getResourceAsStream ("/b.properties")) we can,

How does the path to the properties file under the other JAR packages indicate???

The test found that the root directory of the third-party jar package and SRC are the same directory (of course, this jar package in classpath (⊙o⊙) AH).

My test was done in eclipse, with the A.jar package in the classpath of the project, with only one b.properties file in the jar package.

In the test code, the direct use of Prop.load (This.getclass (). getResourceAsStream ("/b.properties") is also available for loading into the b.properties file.

It is concluded that the third-party jar is similar to the project directory, where the files (folders) inside the jar package are placed directly underneath the project SRC.

If you want to load the "/resource/c/d/e.xml" inside the A.jar package, it is this.getclass (). getResourceAsStream ("/resource/c/d/ E.xml ");

Of course do not use Fileinputsteam ("/resource/c/d/e.xml") is not read (⊙o⊙) OH

(Note: Personal opinion, for reference only)

Java reads configuration files from other jar packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.