"Go" in a MAVEN project package configuration files into a jar package

Source: Internet
Author: User

Reference Blog: http://blog.csdn.net/ciedecem/article/details/10382275

Problem:

The project needs to be loaded with JSON data from the file and placed in the Conf directory.

In the program

XXX.class.getResource ("App.json");

XXX.class.getClassLoader (). getResourceAsStream ("App.json")

Can not find the file, reported NullPointerException. The ClassLoader can only load files under the Classpath directory and cannot read files outside the jar file in either way.

Solution:

Reference Online Blog ... Knowing that the MAVEN project will package all the files in the Src/main/resources directory into a jar, the method above will work. So build the resources directory, found that it is indeed feasible!

Attached: class loader loading file mode

[Java]View PlainCopy
    1. <span style="WHITE-SPACE:PRE;" > </span>inputstream InputStream = emojiutil.  Class.getclassloader (). getResourceAsStream ("Emoji.json");
    2. BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream));
    3. StringBuilder STRSB = new StringBuilder ();

"Go" in a MAVEN project package configuration files into a jar package

Related Article

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.