*
The moose likes IDEs, Version Control and other tools and the fly likes In Eclipse, how to add log4j.properties to classpath ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "In Eclipse, how to add log4j.properties to classpath ?" Watch "In Eclipse, how to add log4j.properties to classpath ?" New topic
Author

In Eclipse, how to add log4j.properties to classpath ?

ben oliver
Ranch Hand

Joined: Mar 28, 2006
Posts: 374
I want to use apache common logging methods, and I think I need to create a "log4j.properties" file and add it to the classpath for my Eclipse project. When I clicked a project, then click "java bulid Path", it allows me to add "JAR", "add Library", "add variable". But how to add a property file to classpath ?

Thanks.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The classpath never includes specific files. It includes directories and jar files. So, put that file in a directory that is in your classpath.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Log4j properties aren't (normally) used in developing apps (unless you're debugging Eclipse itself!). So what you really want to to build the executable Java app (Application, WAR, EAR or whatever) and include the Log4j properties in the runtime classpath.

For WARs, this would be the WEB-INF/classes directory of the WAR you create. For an executable JAR (standalone app), I think it's the root of the JAR.


Customer surveys are for companies who didn't pay proper attention to begin with.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

And if your project is a regular Java application and not a web application, then simply putting the properties file into the project at the root level causes it to be in the project's classpath.
Luis Colorado
Greenhorn

Joined: Feb 01, 2005
Posts: 2
Originally posted by Tim Holloway:
Log4j properties aren't (normally) used in developing apps (unless you're debugging Eclipse itself!). So what you really want to to build the executable Java app (Application, WAR, EAR or whatever) and include the Log4j properties in the runtime classpath.

For WARs, this would be the WEB-INF/classes directory of the WAR you create. For an executable JAR (standalone app), I think it's the root of the JAR.


I have a question and a correction. The question is: why Log4j properties are not used in developing apps?

The correction: don't place log4j.properties or any other properties file in the WEB-INF/classes directory. I think that by default the output folders get scrubbed clean every time you do a full rebuild. This will delete your properties file. You may disable this behavior by going to Window->Preferences->Java->Compiler->Building->Output folder, and uncheck "Scrub output folders when cleaning projects".

I found that the best place to put your properties file is under the src directory. If you put it there, it will automatically be put in your output directory.

Peace,

Luis
[ May 21, 2008: Message edited by: Luis Colorado ]
Velayudhaperumal bhagavathikannu
Greenhorn

Joined: Nov 27, 2005
Posts: 15
hi ben, try this ...

1. create a log4j.properties file like this:

log4j.rootCategory=TRACE, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

2. zip the file and rename it .jar (log4jprops.zip => log4jprops.jar), put the file in a folder you like (example: /workspace/log4jprops/log4jprops.jar)
3. in Eclipse: Run, Debug, click the configuration you want, select Classpath tab
4. click on the User Entries item in the tree view to select it
5. click advanced, select "Add Classpath Variables" radio, and click OK
6. select or create a variable named LOG4J_PROPS, and point it to the JAR created before.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Luis Colorado:

I found that the best place to put your properties file is under the src directory. If you put it there, it will automatically be put in your output directory.


Agreed. Also note that in Eclipse you can have more than one source folder.

Another option is putting it into a class folder (to be configured in the library tab of the build path). In that case, it won't be copied to the output folder.

Putting the file into a jar would work, too, of course, but sounds like overkill to me.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Originally posted by Luis Colorado:


I have a question and a correction. The question is: why Log4j properties are not used in developing apps?
[ May 21, 2008: Message edited by: Luis Colorado ]


Sorry about the misunderstanding. I didn't mean you don't control logging for apps being developed. In fact, I generally do.

What I was trying to say is that logging is for the app itself, not for Eclipse. The only log Eclipse cares about it its own log, and unless you're debugging Eclipse, the less seen of that log, the better.
igor bolsovitch
Greenhorn

Joined: Feb 22, 2009
Posts: 12
Hi,

I'm facing the same problem in a Maven project under Eclipse.

My log4j.properties is under src/main/resources/ so it is automatically pushed to my output directory : target/classes

When I run my small main class from Eclipse I get an FileNotFoundException about log4j.properties but the stronge thing is that the had been parsed I can see the formated output of log4j




If I run my main class from the command line, than I have no Exception (Which is normal because log4j.properties is located under classes)

this is my main :


@ Paul Clapham
And if your project is a regular Java application and not a web application, then simply putting the properties file into the project at the root level causes it to be in the project's classpath.


Yes it worked for me also but I don't want to keep two versions of my log4j.properties (one under resources and the other into the project at the root level) I prefere keep the structure given by maven

What causes Eclipse to be blind about log4j.properties ?

Thanks
anver sadhat
Greenhorn

Joined: Feb 10, 2010
Posts: 4
Hi All,

I prefer to have the log4j.properties outside the Stand Alone App/WAR/EAR. The following is the practice I follow

Create a common folder [that needs to be outside your application]

/webdocs/apps/yourappname/log4j.properties or [any other properties files that often changes]

Now we need to point this folder in the classpath.

To set classpath in MyEclipse for stand alone application.
1. Click the "open run dialog"
2. Go to the "classpath" tab
3. Select the "User Entries" and click the "Advanced" button on the right side.
4. Now select the "Add External folder" radio button. Browse the folder (here /webdocs/apps/yourappname) and click OK button.

To set classpath in MyEclipse for WEB application [ Example : Tomcat ]

1. Select the "Configure server" from the tool bar
or Go to the "Servers" tab (adjacent to the console tab) from the bottom panel, Now click the "configure" button
[the first tool button in this panel]
2. Now select ( and expand ) the Server which you are using, for example tomcat.
3. Now select the "Paths" option.
4. Click the "Add DIR" from the - "Append to classpath:" section.
5. Browse to the folder here /webdocs/apps/yourappname/

By placing all you configuration and application supporting files outside the WAR will help a lot. We do not need to compile the code and redeploy. Just Restarting the server will do.

Anver Sadhat
Brad Markel
Greenhorn

Joined: Mar 02, 2010
Posts: 5
Ilja Preuss wrote:

Another option is putting it into a class folder (to be configured in the library tab of the build path). In that case, it won't be copied to the output folder.

Putting the file into a jar would work, too, of course, but sounds like overkill to me.


May I please ask a follow-up question for this?
Let's say that you have a project in Eclipse with a utility JAR, an EJB, and a web project. Is there a way to create a class folder in Eclipse so that all projects in the EAR share the same log4j.properties file?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
You can place the file in a Jar file, and place that in the lib directory of the root of the Ear file.
Brad Markel
Greenhorn

Joined: Mar 02, 2010
Posts: 5
Does that mean that I should create a separate utility JAR project in Eclipse and add it to the project? I tried adding the class folder as suggested to my existing utility JAR, which works well when I run unit tests for that project, but it does not work when I try to run my web application.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I did something similar recently (I wanted to add a Utility Jar to my EE application). Here's what I did:
  • I created a Utility project in Eclipse (File -> New -> Other -> Java EE -> Utility Project)
  • I named the project 'Utility'
  • I selected 'Add project to EAR' and selected my EE application's Ear

  • On the Project Properties for the EE application, I navigated to Java EE Module Dependencies, located the Utility Jar file and selected the 'In lib dir' check box.

    This caused the generated utility Jar file to be placed in the Ear file's 'lib' directory when the EAR file was created (using Export -> EAR). If you create your Ear file using Ant or Maven, you will need to update those scripts.


    Brad Markel
    Greenhorn

    Joined: Mar 02, 2010
    Posts: 5
    Mark,

    That was an excellent suggestion, so I tried it - but now, the EJB that's packaged in the same EAR can't find the properties file or the rest of the classes that are packaged in the same JAR (NoSuchClass/class not found exceptions occur when starting the app). I feel like we're close. Do you have any other suggestions?

    Thanks
    Mark E Hansen
    Ranch Hand

    Joined: Apr 01, 2009
    Posts: 643
    If all you did was place the properties file in a Jar file, and place that Jar file in the 'lib' directory of the Ear I can't see how this would now cause your EJB classes to no longer be able to find other classes. It seems something is not set up correctly.

    Did you place other things in this Utility Jar file? Note that classes in EAR/lib/utility.jar will not be able to see classes in your EJB-Jar or Web.war files, etc. Is this perhaps the problem you're having?

    Perhaps you can show a directory listing of your Ear file contents, along with explanation of what application components are in which files, etc., and we can see how it all lays out.
    Brad Markel
    Greenhorn

    Joined: Mar 02, 2010
    Posts: 5
    You are correct that there are other classes in the JAR file besides just the log4j.properties file. For example, there is code to do much of my business logic in that same JAR (the EJB's extend some of the classes in this utility project). Unfortunately, I cannot show you a complete listing of the EAR file, but I can give you an example which mirrors the structure:



    When I deploy the project to my Java EE server and I add it to the "Web Libraries" section, it fails to start, throwing NoClassDefFound exceptions for InterfaceDAO, WidgetDAO, WidgetCartDAO, and any other class in WidgetCorpCore.jar. I added the config folder (not a source folder) as a Class Folder as Ilja originally suggested. When running WidgetDAOTest as a JUnit test, it runs just fine. The only way that I've found to get the application to start is to place a second copy of the log4j.properties file in the src directory of WidgetCorpWeb.war.

    Thanks again for the help.
    Mark E Hansen
    Ranch Hand

    Joined: Apr 01, 2009
    Posts: 643
    Let me just say again that if you place classes in a Jar in the 'lib' directory of the Ear, those classes will not see any classes in the EJB-jar or application-web.war files. The classpath just don't go that way.

    You asked how to get the log4j.properties file on the classpath of the entire EE application. I suggested that you place it in a Jar in the 'lib' directory of the Ear. I didn't mean that you should place all your classes there as well.

    Read up on how the class loaders are created for Enterprise applications and you'll see why. I think there are some FAQ entries on the Ranch that talk about this very topic - that would be a good start.

    roger wong
    Ranch Hand

    Joined: Oct 21, 2009
    Posts: 31
    Create a source folder and put your log4j.properties in it.
    Tratata Trututu
    Greenhorn

    Joined: Mar 05, 2010
    Posts: 1
    You can specify location with VM argument like this:
    -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"
     
    Consider Paul's rocket mass heater.
     
    subject: In Eclipse, how to add log4j.properties to classpath ?
     
    Similar Threads
    Not able to save logs into .log file
    hibernate and eclipse
    How to configure the subprojects as one project in eclipse using Ant?
    WAS 5.0 & Log4j
    Can the path of lo4j properties file be relative to classpath?