Wednesday, December 7, 2011

java.io.FileNotFoundException: log4j.xml (No such file or directory)

If you get below mentioned error

log4j:ERROR Could not parse url [file:log4j.xml].
java.io.FileNotFoundException: log4j.xml (No such file or directory)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.(FileInputStream.java:106)
        at java.io.FileInputStream.(FileInputStream.java:66)
        at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
        at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
        at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
        at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
        at org.apache.log4j.xml.DOMConfigurator$2.parse(DOMConfigurator.java:612)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:711)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:618)
        at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:470)
        at org.apache.log4j.LogManager.(LogManager.java:122)
        at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:254)
        at org.quartz.impl.StdSchedulerFactory.(StdSchedulerFactory.java:274)

Possible reason could be JVM is not able to find the provided file log4j.xml. Make sure the file is present in the location and You could pass the file in command line as shown below.

java -Dlog4j.configuration=file:/home/log4j.xml com.TestJava

OR add directly in your Java code.

System.setProperty("log4j.configuration", "/home/log4j.xml");

If you are using Eclipe IDE you can pass through VM arguments by steps shown below

Right click on Java -> Run As -> Run Configurations.. -> Arguments tab -> VM arguments

add the below code

-Dlog4j.configuration=file:/home/log4j.xml

Apply -> Run