views:

60

answers:

2

I feel like a schmuck for asking this, but I'm tired of guessing.

I'm running a simple java program from the IntelliJ IDE using the Run->Run menu. No problem, 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. I really hate this part of intellij. How about an "Add to classpath" option somewhere? /fume /vent /rant

+2  A: 

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
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
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
It's there. I appreciate the help. I'll keep digging.
Tony Ennis
+1  A: 

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
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
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
Could you please send the sample project with the exact steps to reproduce the problem to [email protected]?
CrazyCoder
See the comment I attached to the main question.
Tony Ennis