Solve the problem that IntelliJ IDEA cannot read the configuration file.

Source: Internet
Author: User

Solve the problem that IntelliJ IDEA cannot read the configuration file.

Recently, in Mybatis, follow the video to create a configuration file in a package of the project and read the configuration file. However, an exception is always prompted.

The official code is used to read the configuration file:

String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);SqlSession session = sqlSessionFactory.openSession();   

After thinking over and over again, the video uses eclipse as the ide, And the configuration file (. propoties and. after compilation, it is automatically copied to the compiled folder, that is, the classes directory. Therefore, the code can be loaded to the configuration file through a path.

Now we use IntelliJ IDEA. The idea file method for these configurations is obviously different from eclipse. There is a concept of Content Roots in idea. You need to configure the corresponding Content Roots for each folder. Content Roots includes resources, sources, and tests.

:

  • If the java folder is marked as Sources, all the subfolders below are Sources, and the. class file is generated after compilation.
  • The Resources folder is marked as Resources. The configuration files in this folder are automatically copied to the compilation folder during compilation.

For idea, there are the following solutions.

  1. Put the configuration file in the Resources folder and add the virtual path of the configuration file to the code. Select a file and right-click Copy Reference to obtain the virtual path of the file.
  2. Place the configuration file in the com. zjut. ssm. config package and modify the Content Roots in the config folder to Resources. After compilation, the code can also be identified.

The above two solutions apply to the make project method to compile the project.

For maven projects. You can also configure pom. xml.

 <build>    <finalName>springmvc-study</finalName>    <resources>        <resource>            <directory>${basedir}/src/main/java</directory>            <includes>                <include>**/*.properties</include>                <include>**/*.xml</include>            </includes>        </resource>        <resource>            <directory>${basedir}/src/main/resources</directory>        </resource>    </resources> </build>

In this way, you can use the maven compilation command to compile the project without setting idea, and copy the configuration file to the compiled folder.

Use IntelliJ IDEA 13 to build an integrated Android Development Environment graphic tutorial

IntelliJ IDEA 12 create a Web project graphic tutorial

Graphic tutorial for Android program development using IntelliJ IDEA

IntelliJ IDEA 12 haXe NME application Configuration Guide

IntelliJ IDEA test mode for running the Play Framework

Install IntelliJ IDEA 12 on Ubuntu 13.04

IntelliJ IDEA 12 create a Java Web project managed by Maven (illustration)

IntelliJ IDEA common shortcut keys and tips

IntelliJ IDEA details: click here
IntelliJ IDEA: click here

This article permanently updates the link address:

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.