• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 375
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classpath never includes specific files. It includes directories and jar files. So, put that file in a directory that is in your classpath.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 650
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 650
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.

     
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Create a source folder and put your log4j.properties in it.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You can specify location with VM argument like this:
    -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"
     
    reply
      Bookmark Topic Watch Topic
    • New Topic