Add a properties file to IntelliJ's classpath

后端 未结 10 2254
栀梦
栀梦 2020-11-28 19:20

I\'m running a simple Java program from the IntelliJ IDE using the Run->Run menu. It works fine. Now I want to add log4j logging.

I added a resources folder under my

相关标签:
10条回答
  • 2020-11-28 20:04

    If you ever end up with the same problem with Scala and SBT:

    • Go to Project Structure. The shortcut is (CTRL + ALT + SHIFT + S)

    • On the far left list, choose Project Settings > Modules

    • On the module list right of that, select the module of your project name (without the build) and choose the sources tab

    • In middle, expand the folder that the root of your project for me that's /home/<username>/IdeaProjects/<projectName>

    • Look at the Content Root section on the right side, the red paths are directories that you haven't made. You'll want to put the properties file in a Resources directory. So I created src/main/resources and put log4j.properties in it. I believe you can also modify the Content Root to put it wherever you want (I didn't do this).

    • I ran my code with a SBT configuration and it found my log4j.properties file.

    0 讨论(0)
  • 2020-11-28 20:05

    Faced a similar challenge adding files with .ini extensions to the classpath. Found this answer, which is to add it to Preferences -> Compiler -> Resource Patterns -> [...] ;*.ini

    0 讨论(0)
  • 2020-11-28 20:06

    I have the same problem and it annoys me tremendously!!

    I have always thought I was surposed to do as answer 2. That used to work in Intellij 9 (now using 10).

    However I figured out that by adding these line to my maven pom file helps:

    <build>
      ...
      <resources>
        <resource>
          <directory>src/main/resources</directory>
        </resource>
      </resources>
      ...
    </build>
    
    0 讨论(0)
  • 2020-11-28 20:07

    I spent quite a lot of time figuring out how to do this in Intellij 13x. I apparently never added the properties files to the artifacts that required them, which is a separate step in Intellij. The setup below also works when you have a properties file that is shared by multiple modules.

    • Go to your project setup (CTRL + ALT + SHIFT + S)
    • In the list, select the module that you want to add one or more properties files to.
    • On the right, select the Dependencies tab.
    • Click the green plus and select "Jars or directories".
    • Now select the folder that contains the property file(s). (I haven't tried including an individual file)
    • Intellij will now ask you what the "category" of the selected file is. Choose "classes" (even though they are not).
    • Now you must add the properties files to the artifact. Intellij will give you the shortcut shown below. It will show errors in the red part at the bottom and a 'red lightbulb' that when clicked shows you an option to add the files to the artifact. You can also go to the 'artifacts' section and add the files to the artifacts manually.

    enter image description here

    0 讨论(0)
提交回复
热议问题