137

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 project root. I added a log4j.properties file in that folder. I changed the code to log something.

What is the right way to tell IntelliJ to include the resources folder in the classpath so the properties file is seen?

With IntelliJ 8 I could guess like a drunk monkey and eventually get it to work. I have 9 now and I am wholly unsuccessful. I've been trying for an hour. How about an "Add to classpath" option somewhere? /fume /vent /rant

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Tony Ennis
  • 12,000
  • 7
  • 52
  • 73
  • Ok, the situation is resolved. This is a new installation of IntelliJ - log4J is NOT included by default. In my code I had imported the stock logger, not log4j. The stock logger supports similar methods so it wasn't obvious I was using the wrong logger! No surprise why it wasn't reading the log4j properties file or reporting that I needed to configure log4j. What a kick in the teeth! – Tony Ennis Sep 22 '10 at 15:00
  • Found a Good description for a beginner to log4j http://tuttlem.github.io/2015/02/08/start-logging-with-log4j2.html – Rahal Kanishka Jun 16 '17 at 06:16

11 Answers11

100

Try this:

  • Go to Project Structure.
  • Select your module.
  • Find the folder in the tree on the right and select it.
  • Click the Sources button above that tree (with the blue folder) to make that folder a sources folder.
ColinD
  • 108,630
  • 30
  • 201
  • 202
  • I've done that about 50 times, I just did it again. My output isn't changing even though I changed the layout conversion pattern to something that would have been obvious. I'm wondering if another log4j.properties is in my classpath. – Tony Ennis Sep 22 '10 at 03:56
  • 31
    Check the Resource patterns in the Compiler settings. Check that "?*.properties" is in there. It's there by default, but that's the only other thing I can think of off the top of my head. – ColinD Sep 22 '10 at 04:02
  • 3
    Alternative to Sourcesbutton - right click whatever Directory youve created and select "Mark Directory As": "Source Root" – mschr Apr 27 '13 at 17:17
  • 12
    Note that if you created your project from a Maven POM, instead of an ad-hoc source structure, this solution will not work. Instead you need to add that directory as a resource to the POM. See Peter Thygesen's answer. – lreeder Aug 25 '13 at 18:07
  • Can also be done programatically via PropertyConfigurator.configure("../conf/log4j.properties") – Jason D Apr 30 '14 at 16:20
  • This works, but not if you need a particular properties file in two different modules, because you will bump into the 'two modules cannot share the same content root' error. In this case you shoudl use the "depedencies tab of module" way. – Julius Nov 14 '14 at 16:03
49

Actually, you have at least 2 ways to do it, the first way is described by ColinD, you just configure the "resources" folder as Sources folder in IDEA. If the Resource Patterns contains the extension of your resource, then it will be copied to the output directory when you Make the project and output directory is automatically a classpath of your application.

Another common way is to add the "resources" folder to the classpath directly. Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder.

Yet another solution would be to put the log4j.properties file directly under the Source root of your project (in the default package directory). It's the same as the first way except you don't need to add another Source root in the Module Paths settings, the file will be copied to the output directory on Make.

If you want to test with different log4j configurations, it may be easier to specify a custom configuration file directly in the Run/Debug configuration, VM parameters filed like:

-Dlog4j.configuration=file:/c:/log4j.properties.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • I have done your paragraph 2 and 3 to no effect. I'm sure the suggestions _work_ it's just that they had no effect - it behaves like there's another log4j.properties file in the classpath. But I can't see it anywhere. If I remove my log4j properties file altogether I don't get the "you must configure log4j warning" in the console. I'm using the free IntelliJ (and version 9.x) for the first time, so maybe it has something to do with that. – Tony Ennis Sep 22 '10 at 13:32
  • 1
    Using the explicit -D VM parameter also had no effect. At this point I can only surmise I have fallen from the top of the 'stupid tree' and hit every branch on the way down! I think I'll go trawl around at JetBrains and ask around... – Tony Ennis Sep 22 '10 at 13:57
  • Could you please send the sample project with the exact steps to reproduce the problem to support@jetbrains.com? – CrazyCoder Sep 22 '10 at 14:42
  • I was passing -Dlog4j.configuration=file:/c:/log4j.properties in program arguments. VM parameters did the trick for me. – Ajak6 Jun 03 '16 at 21:23
48

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>
Peter Thygesen
  • 481
  • 4
  • 2
21

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

Julius
  • 2,784
  • 6
  • 32
  • 54
14

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

mateuscb
  • 10,150
  • 3
  • 52
  • 76
prototype
  • 7,249
  • 15
  • 60
  • 94
6

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.

enter image description here

user2361174
  • 1,872
  • 4
  • 33
  • 51
2

For those of you who migrate from Eclipse to IntelliJ or the other way around here is a tip when working with property files or other resource files.

Its maddening (cost my a whole evening to find out) but both IDE's work quite different when it comes to looking for resource/propertty files when you want to run locally from your IDE or during debugging. (Packaging to a .jar is also quite different, but thats documented better.)

Suppose you have a relative path referral like this in your code:

new FileInputStream("xxxx.properties");

(which is convenient if you work with env specific .properties files which you don't want to package along with your JAR)

INTELLIJ

(I use 13.1 , but could be valid for more versions)

The file xxxx.properties needs to be at the PARENT dir of the project ROOT in order to be picked up at runtime like this in IntelliJ. (The project ROOT is where the /src folder resides in)

ECLIPSE

Eclipse is just happy when the xxxx.properties file is at the project ROOT itself.

So IntelliJ expects .properties file to be 1 level higher then Eclipse when it is referenced like this !!

This also affects the way you have to execute your code when you have this same line of code ( new FileInputStream("xxxx.properties"); ) in your exported .jar. When you want to be agile and don't want to package the .properties file with your jar you'll have to execute the jar like below in order to reference the .properties file correctly from the command line:

INTELLIJ EXPORTED JAR

java -cp "/path/to_properties_file/:/path/to_jar/some.jar" com.bla.blabla.ClassContainingMainMethod

ECLIPSE EXPORTED JAR

java -jar some.jar

where the Eclipse exported executable jar will just expect the referenced .properties file to be on the same location as where the .jar file is

DataHacker
  • 276
  • 2
  • 10
  • Yes, logging.properties, unlike log4j2.xml or log4j.properties, must be found in the IntelliJ module root. Also unlike Log4j, which just works, I had found I still had to jury-rig to run inside IntelliJ (Eclipse would be the same story, this is Java logging's fault), see http://stackoverflow.com/questions/960099/how-to-set-up-java-logging-using-a-properties-file-java-util-logging#3750129. One alternative would be to fix the run/debug configuration up with -Djava.util.logging.config.file=, which I find rather inconvenient as I'd rather have configuration in code or in an expected property file. – Russ Bateman Oct 02 '15 at 21:19
2

Right-click on your directory and from Mark directory as select Resources root as below:

enter image description here

Mehdi Golzadeh
  • 2,594
  • 1
  • 16
  • 28
1

Perhaps this is a bit off-topic, seeing as the question has already been answered, but I have experienced a similar problem. In my case only some of the unit test resources were copied to the output folder upon compilation. My persistence.xml in the META-INF folder got copied but nothing else.

In the end I "solved" the problem by renaming the problematic files, rebuiling the project and then changing the file names back to the original ones. Do not ask me why this worked but it did. My best guess is that, somehow, my IntelliJ project had gotten a bit out of sync with the file system and the renaming operation triggered some kind of internal "resource rescan".

1

This is one of the dumb mistakes I've done. I spent a lot of time trying to debug this problem and tried all the responses posted above, but in the end, it was one of my many dumb mistakes.

I was using org.apache.logging.log4j.Logger (:fml:) whereas I should have used org.apache.log4j.Logger. Using this correct logger saved my evening.

avp
  • 2,892
  • 28
  • 34
0

I had a similar problem with a log4j.xml file for a unit test, did all of the above. But figured out it was because I was only re-running a failed test....if I re-run the entire test class the correct file is picked up. This is under Intelli-j 9.0.4

James B
  • 3,692
  • 1
  • 25
  • 34