How to load log4j.properties

how to load log4j.properties file in my java code ?
i have put log4j.properties in c:\property\log4j.properties directory.
how do i load this properties file ?

Normally you don't have to load it explicitely at
all. Just place it in your classpath and use log4j,
it should be loaded automagically.so, at run time i should use
java -classpath .;c:\property\log4j.properties;c:\log4j.jar; MyClass
is this the syntax ?

Similar Messages

  • How to load a properties from path that is relative to the classpath

    Hello;
    I need to load a set of properties from a propertie file from inside and EJB. I need to do so that I don't set the full absoulte path of the property file. In brief I don't want to be dependent on the file system struture instead I want to read the property file relatively to the package path name.
    Here is my code:
    package packagename.com;
    import packagename.com.*;
    import java.util.*;
    import java.io.*;
    public class PropDemo {
    public static void main(String[] args) {
    public static final String PROP_FILE_NAME
    = "configuration.properties";
    File inputFile = new File(PROP_FILE_NAME);
    Properties prop = new Properties();
    try {
    FileInputStream fis = new FileInputStream(inputFile);
    prop.load(fis);
    } catch (Exception e) {
    Any body have an idea how to so.
    Regards
    mal

    I realize that you are trying to be a pain in the arseNot really. I know that I spent about a day figuring it out and that was after skirting around the issue for a about a year.
    with that reply, but we can look here:
    public void load(InputStream inStream) throws
    IOExceptionReads a property list (key and element pairs) from the
    input stream. The stream is assumed to be using the
    ISO 8859-1 character encoding.
    The original post suggests the author already knows how to load from a file. So this doesn't help at all.
    And possibly GASP cross reference this entry in
    Class:
    public InputStream getResourceAsStream(String name)Finds a resource with a given name. This method
    returns null if no resource with this name is found.
    The rules for searching resources associated with a
    given class are implemented by the defining class
    loader of the class.
    Now you may equivocate that the docs don't
    specifically state that the classpath is where
    resources are searched for, but I would maintain that
    any Java developer worth anything should know this.Really? So where, in the above entry, does it explain the impact of the "/" character at the beginning of the string?
    I know how classes are searched for. I know the impact of the class path. How that impacts a 'resource' is less clear. Particularily since the "/" character has nothing to do with loading classes.
    >
    This should be Java 101. Advanced Topics should be
    something that affect people who have programmed Java
    for years. (This is usually something that first year
    programmers work out). If this is representitive of
    the kinds of questions that qualify as "Advanced" then
    this forum will become basically useless.So an opposing opinion...this is an advanced topic. Nothing in the java docs makes it clear. And it is even less clear the context of a container.

  • How to load a properties file?

    Hello Everyone,
    We are experiencing difficulties when trying to load a properties file from a directory other then the one where the servlet is located. We are using the following code for this purpose:
    private final String PROP_FILE_NAME = "my.properties";
    Properties props = new Properties();
    try {
    props.load(getClass().getResourceAsStream(codeBase + PROP_FILE_NAME));
    } catch(IOException e) {
    //handle error ...
    where codeBase is either physical directory path to the properties file or the URL. In either case we get a NullPointer exception which means that file is not found. But when we copy the properties file into the same directory where the servlet resides, everything works fine.
    Does anybody have an idea as to how to resolve our problem?
    Any help will be greatly appreciated!
    Thanks in advance!
    Y.M.

    URLs try to address this issue for cross-platform file reference.
    I'd try doing something like:
    URL u = new URL(codeBase + PROP_FILE_NAME);
    props.load(getClass().getResourceAsStream(u));
    Regards,
    Eric

  • How to load a .properties file by giving relative path in a JSP file.

    Hey guys,
    I'm trying to load a .properties file in a JSP using relative path...
    It is not taking the properties file..
    What is the problem...?
    Any solution..?
    Thanks a lot.

    Yes I tried that also.
    Your path was something like this ?
    http://localhost:8080/Database/a.properties ?
    Whether this path will change after client installation ? if no then what's the problem ?
    For client side installation how you are going to set your conf directory for your application ?
    You are not going to give client installation page ?which will have all the values to be set like conf path. database,user name(s),log folder,temp folder etc ? Why you are keeping everything on server ?
    Check if this is useful,
    String context = request.getContextPath();
    String rmtAdd = request.getRemoteAddr();
    String host = request.getRemoteHost();
    out.println("http://"+rmtAdd+context+"<BR>");
    out.println("http://"+host+context);
    "http://"+rmtAdd+context
    Edited by: kanad on Jan 12, 2008 1:29 PM

  • How to load Java properties file dynamically using weblogic server

    Hi,
    We are using Java properties file in Java code. Properties in java properties file are frequently modified. If I keep these properties file in project classpath or as part of war, I will have to redeploy application after each change.
    We are using Weblogic Server.
    Can you please suggest me how can this properties file be loaded at weblogic server startup. Then in that case, how to refer property file in Java code?
    What is the best practice for this?
    Thanks,
    Parshant

    Another alternative is, keep the property file in any pre-defined location. Write a class which reads the properties from the file and returns the one which is requested by caller and deploy this class. Whenever you have to change the properties just update the property file on server and next call to fetch the property should return the updated one.
    Downside of this approach is file I/O everytime. To overcome that you can actually "cache" the properties in a hashmap. Basically when any property if requested, first check the hashmap, if not found then only read from property file and also update in hash map. Next time same property will be returned from hash map itself. The hash map will be cleared at every server restart since its in the memory. You will also need to build a method to clear the hashmap when you update the values in the property file on server.
    This solution would be suitable for small size files and when network overhead of calling a DB needs to be avoided.

  • How to load a Properties file : Very URGENT

    I have properties file ie., SQLProperties.properties
    SQLProperties.properties
    logdetails = SELECT USERNAME AND PASSWORD FROM USER_TBL
    I want to load this file .
    I used like this
    public static Properties prop = null;
    strKey ="logdetails";
    FileInputStream fis = new FileInputStream("SQLProperties.properties");
    prop.load(fis);    // Since load parameter is file input stream
    prop.getProperty(strKey);But it is coming error while loading

    You did not initialize prop.
    //public static Properties prop = null;
    public static Properties prop = new  Properties ();
    strKey ="logdetails";
    FileInputStream fis = new FileInputStream("SQLProperties.properties");
    prop.load(fis);    // Since load parameter is file input stream
    prop.getProperty(strKey);
    [/prop]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Log4j.properties - ClassNotFoundException

    I'm using log4j and Weblogic 6.0. I initialize log4j using a startup servlet that loads the log4j.properties file located in WEB-INF\classes. Log4j gets initialized fine, except that I always get
    a ClassNotFound exception because weblogic
    tries to load log4j.properties as a class each time it initializes a webapp. How do I
    remedy this? Thanks.

    Please post start-up servlet code.
    Karen Yang wrote:
    I'm using log4j and Weblogic 6.0. I initialize log4j using a startup servlet that loads the log4j.properties file located in WEB-INF\classes. Log4j gets initialized fine, except that I always get
    a ClassNotFound exception because weblogic
    tries to load log4j.properties as a class each time it initializes a webapp. How do I
    remedy this? Thanks.

  • How to load data with carriage return through DRM action script ?

    Hello,
    We are using DRM to manage Essbase metadata. These metadata contain a field for member formula.
    Currently it's a string data type property in DRM so we can't use carriage return and our formula are really hard to read.
    But DRM support other data type property : memo or formatted memo where we can use carriage return.
    Then in the export file, we have change the record delimiter to an other character than CRLF
    Our issue : we are regularly using action script to load new metadata => How to load data properties with carriage return using action script ? There is no option to change the record delimiter.
    Thanks!

    Hello Sandeep
    here what I want to do through action script : loading a formula that use more on than one line:
    Here, I write my formula using 4 lines but action script cannot load since one line = 1 record.
    ChangeProp|Version_name|Hier_name|Node_name|Formula|@round(
    qty*price
    *m05*fy13

  • How to Load a propety file (resources/toto.properties) in my jnlp ?

    Hi,
    I have to deploy a java application using JWS for the first time and i encounter a problem with my properties file.
    this is the story:
    I have a properties file which contains 1 line:
    repsrc = X:\\Documents and Settings\\aberghousse\\Bureau\\src\\When I deploy my application the jnlp can't open this file, this is the class that I use to load the properties file:
    public class LoadProp extends Properties {
         private static final long serialVersionUID = 1L;
         final static Logger log1 = Logger.getLogger(LoadProp.class);
         public LoadProp() throws IOException {
              // Read properties file.
              super();
              InputStream in = getClass().getResourceAsStream("/pathname.properties");
              try {
                  this.load(in);
              } catch (IOException e) {
                   log1.error("Load properties error", e);
              }finally {
                  in.close();
    }the pathname.properties is located in conf/pathname.properties and conf directory is in my CLASSPATH.
    In my sources to load a key I use this :
    File src = new File(new LoadProp().getProperty("repsrc ")So why this Exception :(
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
         at java.io.File.<init>(Unknown Source)
         at com.experian.IhmFdi.GetFiles.findFiles(GetPdfFiles.java:30)
         at com.experian.IhmFdi.GetFiles.<init>(GetPdfFiles.java:26)
         at com.experian.IhmFdi.Ihm.<init>(Ihm.java:99)
         at com.experian.IhmFdi.Ihm$16.run(Ihm.java:860)
         at java.awt.event.InvocationEvent.dispatch(Unknown Source)
         at java.awt.EventQueue.dispatchEvent(Unknown Source)
         at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
         at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
         at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
         at java.awt.EventDispatchThread.run(Unknown Source)
    Thank you for your help.
    null
    null

    yes perhaps no one know how we can to do, but i'm using URL class to load my file so I suppose that I have two mode
    1- offline : so i search file localy
    2- online : using URL
    this is a solution that works well for me I hope this help.
    aa
    Edit :
    this is an example : your source:
    URL prop = new URL(System.getProperty("propertyFile"));your jnlp file :
    <property name="propertyFile" value="http://ip:8080/conf/yourfile.properties"/>Edited by: Aberghouss on Jan 7, 2008 2:33 PM
    Edited by: Aberghouss on Jan 7, 2008 2:35 PM

  • How do I load a properties file that is in the same package?

    I have the following package:
    com.company.ldap
    In the package I have:
    main.java
    settings.properties
    I want to load the values in the settings.properties file in code, such as:
    propertiesFile = "settings.properties";
            try {
                properties.load(new FileInputStream(new File(propertiesFile)));
            } catch (IOException e) {
                ... error logging logic ...
            }I keep returning "The system cannot find the file specified".
    How do I properly load a properties file in a class file within the same package?
    Thanks,
    --Todd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    Put the file in the classpath (such as in the parent directory of com/company/ldap). Then obtain an input stream to that file like this:
    InputStream stream = getClass().getResourceAsStream("/settings.properties");
    Then use that stream in the properties.load method. Remember to close the stream as well.

  • How to load properties file

    Hello Everyone,
    We are experiencing difficulties when trying to load a properties file from a directory other then the one where the servlet is located. We are using the following code for this purpose:
    private final String PROP_FILE_NAME = "my.properties";
    Properties props = new Properties();
    try {
    props.load(getClass().getResourceAsStream(codeBase + PROP_FILE_NAME));
    } catch(IOException e) {
    //handle error ...
    where codeBase is either physical directory path to the properties file or the URL. In either case we get a NullPointer exception which means that file is not found. But when we copy the properties file into the same directory where the servlet resides, everything works fine.
    Does anybody have an idea as to how to resolve our problem?
    Any help will be greatly appreciated!
    Thanks in advance!
    Y.M.

    HI,
    I always keep my properties in a sub-directory called resources. In this case it is called Aircraft.properties.
    * Holds the ResourceBundle of this object.
         private static ResourceBundle resources;
         static
              try
                   resources = ResourceBundle.getBundle("resources.Aircraft", Locale.getDefault());
              catch (MissingResourceException mre)
                   System.err.println("resources/Aircraft.properties not found");
                   System.exit(1);
    // Here I pick up the data from the properties file
         protected String getResourceString(String nm)
              String str;
              try
                   str = resources.getString(nm);
              catch (MissingResourceException mre)
                   str = null;
              return str;
    // In this case I use menubar as key to my properties
    String[] menuKeys = tokenize(getResourceString("menubar"));
    Hope this will help a bit.
    Klint

  • How to load properties inner Jdeveloper

    Hello,
    I'm trying to load properties from some file using something like this
    "final URL in = this.getClass().getResource("/jfreereport.resources");"
    But this allways returns me null, so this Mean that my properties file was not loaded, I tried add this file to my classpath (throught project properties), and when I run my classpath has the file but my application is not capable to locate my properties, why??
    How is the exactly procedure to load this properties files??
    Thanks in advanc
    Mauricio Alarcon

    Try something like:
      Properties props = new Properties();
      try
        InputStream in = ClassLoader.getSystemResourceAsStream("/jfreereport.resources");
        props.load(in);
        in.close();
      catch(Exception ex)
         //do something
      }

  • How to Enable Log4j in Struts

    Hi,
    I have a struts application and I need to implement a logger for it. I would like to use Log4j but am having problems initializing it.
    Because I am using struts, I do no have access to the Action servlet and so cannot place the intiializing code in the servlets init() method.
    Is there a way of using the struts-confix.xml file to enable Log4j logging ?
    Basically I need the application to load up the log4j.properties file using
    PropertyConfiguratior.configure("log4j.properties");
    But how do I do this in Struts ??
    thanks
    Paul

    Struts core library uses commons logging , some drop log4j.properties files /WEB-INFO/classes, cooomons lgging will pick up and initialize.
    Other way is write a plug-in and load the log4j configuration
    ==============================================
    1. Add plug in to struts-config files like validator plug in
    <plug-in className="com.tests.utl.Log4jPlugin">
    <set-property
    property="log4jconfig"
    value="/WEB-INF/log4j.xml"/>
    </plug-in>
    2. Drop log4j.xml file in "/WEB-INFO" folder
    3. Sample "log4j" plug in java class, here key is define property "log4jconfig" as plugin setter/getter, struts will set the value read from struts-config.
    ================================
    public class Log4jPlugin implements PlugIn {
    private String log4jconfig;
    public Log4jPlugin() {
    public void destroy() {
    System.out.println("Destroying Hello World PlugIn");
    public void init(ActionServlet servlet, ModuleConfig config)
    throws ServletException {
    URL url =null;
    try {
    url = servlet.getServletContext().getResource(log4jconffile);
    System.out.println(" url " + url);
    } catch (MalformedURLException ex) {
    ex.printStackTrace();
    DOMConfigurator.configure(url);
    public String getLog4jconfig() {
    return log4jconfig;
    public void setLog4jconfig(String log4jconfig) {
    this.log4jconfig = log4jconfig;
    }

  • Cost involved in changing the code using log4j.properties to log4j.xml

    i have finished my application, and it was done in such a way that logging is done using log4j but it was using log4j.properties file.
    now i need to change it to log4j.xml. i need to do this at the earliest. so i need to know the cost involved in it?. do i need to change a lot on my app to read the log4j.xml?. In my app there is a function named getProperties, which actually reads the log4j.properties file.
    this is the code of that function
    public static Properties getProperties(String filename,String path) throws myException
    //InputStream is = null;
    FileInputStream is=null;
            Properties props = new Properties();       
            //ClassLoader classLoader = ResourceUtil.class.getClassLoader();
            //is = classLoader.getResourceAsStream(filename);
            File catalinaHome = new  File(System.getProperty("catalina.home"));     
            try {       
    is=new
    FileInputStream(catalinaHome.getCanonicalFile()+path + filename);
    } catch (IOException e) {
            if(is == null)
                String message = "Cannot locate property file " + filename + " in the classpath";
                log.error(message);
            throw new myException(message);
            try
                props.load(is);
            catch (IOException e)
                String message = "Error reading property file " + filename;
                log.error(message);
            throw new myException(message, e);
            finally
                try
                    is.close();
                catch(Throwable t)
                    t.printStackTrace();
            if(props.isEmpty())
                String message = "No properties are defined in " + filename;
                log.error(message);
            throw new myException(message);
            return props;       
    }and my initialize function is
    public static void initialize()
    Properties properties=null;
    try {
    String path="/properties/";
    properties=ResourceUtil.getProperties("log4j.properties",path);
    } catch (myException e2) {
    e2.printStackTrace();
    PropertyConfigurator.configure(properties);
    }Message was edited by:
    xema
    Message was edited by:
    xema
    Message was edited by:
    xema

    >
    1. Restructure log4j.properties into log4j.xml.
    thats done
    2. Move log4j.xml into a directory that's in the
    classpath.
    actually i donot understand what does it mean to move in a directory thats in classpath means. i have to use logging in my webapplication running in tomcat. and my log4j.xml has to reside in catalina.home/properties/. My log4j.properties have been residing here and it had worked well woth the previous function. Now what i did is placed the log4j.xml under properties folder. Now tell me what modifications should i make in the above code.
    3. Remove any code that explicitly configures log4j
    (i.e. the initialize() method) because log4j will
    configure itself from any properties file it finds in
    the classpath, automatically, the first time it is
    called.I prefer not remvoing this function as this function is being called in many of my classes... and we don't have time to edit all...
    please help... and excuse me if i asked some blunders.!!!

  • How to set log4j at runtime

    Hi,
    I am using log4j and want to take the logfile name as a command line argument.
    Can anyone help as to how to set the properties which are done statically in log4j.propreties file....
    I tried
    logProperties.setProperty("log4j.appender.ROOT", "org.apache.log4j.RollingFileAppender");
    logProperties.setProperty("log4j.appender.ROOT.File", fileName);
    logProperties.setProperty("og4j.appender.ROOT.layout", "org.apache.log4j.PatternLayout");
    logProperties.setProperty("log4j.appender.ROOT.layout.ConversionPattern", "%d{ISO8601} - <<<<< [%F][%M] %m >>>>>%n");
    but that doesnot seem to work...as the log file itself is not created.
    ANyone help is appreciated...
    This topic has no replies.

    Hi,
    I am using log4j and want to take the logfile name as a command line argument.
    Can anyone help as to how to set the properties which are done statically in log4j.propreties file....
    I tried
    logProperties.setProperty("log4j.appender.ROOT", "org.apache.log4j.RollingFileAppender");
    logProperties.setProperty("log4j.appender.ROOT.File", fileName);
    logProperties.setProperty("og4j.appender.ROOT.layout", "org.apache.log4j.PatternLayout");
    logProperties.setProperty("log4j.appender.ROOT.layout.ConversionPattern", "%d{ISO8601} - <<<<< [%F][%M] %m >>>>>%n");
    but that doesnot seem to work...as the log file itself is not created.
    ANyone help is appreciated...
    This topic has no replies.

Maybe you are looking for