-1

The server is a simple jetty Server

How to set the log4j.properties file i have a proper log4j properties file,

but while setting the log4j.properties

using the following manner, i have the log4j.properties in my src folder

PropertyConfigurator.configure("log4j.properties");

it works fine when i am working locally, but when i create a jar file and run its throwing an exception like java.io.FileNotFoundException:

i have tried extracting it and created it in another folder called resources and tried accessing that by the following method

PropertyConfigurator.configure("resources/log4j.properties");

even after that its showing the same error

how to export the entire project as a jar file and make this log4j problem to work?

Found another link Log4j Properties in a Custom Place

and in that it is required to set the class path

java -Dlog4j.configuration=conf/log4j.properties -classpath ...

Do not know how to set the -classpath and dont know whether this method will work!!

And even if its exported as a jar file it should work!

Community
  • 1
  • 1
raghul
  • 1,008
  • 1
  • 15
  • 33
  • I am using eclipse with embedded jetty – raghul Oct 06 '12 at 10:55
  • Finally enabled -Dlog4j.debug=true in the VMAgrument, and then placed the log4j.properties file in the src folder and now it works, even if i extract it as a jar on the server. Make sure you set the classpath and path as per mentioned in the tutorial of Tutorialspoint – raghul Oct 12 '12 at 09:00

2 Answers2

3

If the log4j.properties resource directory is on the classpath, you could use:

PropertyConfigurator.configure("classpath:resources/log4j.properties");

To see the working directory for Jetty, you could add:

System.out.println(System.getProperty("user.dir"));

before the PropertyConfigurator.configure statement. This would allow you to see where the property file is located in relation to the server's working directory.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • how to check if my log4j.properties is on the class path? i am very sorry i am new to this so can you kindly tell , how to check if my log4j.properties is on my classpath? or how to set class path? – raghul Oct 08 '12 at 12:37
  • 1
    This is more than I can fit into a comment and probably deserving of a new post. In the meantime here is a [related post](http://stackoverflow.com/questions/5402953/log4j-properties-not-picked-up-from-jar). – Reimeus Oct 08 '12 at 12:49
2

In order to make it work immediatley, you can configure them from code:

Properties props = new Properties();
props.setProperty("<KEY>","VALUE");
PropertyConfigurator.configure(props);

Hardcode the props object with all the properties from log4j.properties file.

This is not the solution you ask, but it might very helpful if you are short on time.

linski
  • 5,046
  • 3
  • 22
  • 35