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

Similar Messages

  • 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]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • 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 ?

  • 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 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 read the properties file available in Server File structure in webdy

    hi all,
    I have developed one webdynpro application. In this application i need to access mdm server to continue. For getting the connection i need to pass the IP addresses.
    Can i have code  how to read the properties file which is residing in the server file. with out included along with the application. keeping some where in the file structure in the server. I want to read that properties file by  maintain the iP addresses and users in  properties file based on the key i want to read like below.
    servername="abcServer"
    username="john"
    password="test123"
    Please send me the code how to read this properties file from the file structure and how to read this values by key  in webdynpro application
    Regards
    Vijay

    Hi Vijay,
    You can try this piece of code too:
    Properties props = new Properties();
    //try retrieve data from file
    //catch exception in case properties file does not exist
    try {
             props.load(new FileInputStream("c:\property\Test.properties")); //File location
             String serverName = props.getProperty("servername"); //Similarly, you can access the other properties
             if(serverName==null)
               //....do appropriate handling
         }     catch(IOException e)
                   e.printStackTrace();
    Regards,
    Alka.

  • How to read a properties file

    hi
    I have a properties file.
    I am using load method to load the properties file but i have encountered a problem here.
    the properties file has space like
    FIRST PROP=xyz
    SECOND PROP=abc
    my question is how to read the file
    thank u

    Either format it properly--I think a \ before the space will work--or write your own method to use in place of Properties.load.
    (Or, I suppose, you could inject your own InputStram that translates the lines on the fly to include the \, but that would probably be more trouble than it's worth.)

  • How to read from properties file

    Hi,
    I am using JSR 168.
    while creating a new portlet, a folder gets created with the name as "portlet". Under which is resource package and <PortletName>Bundle.java.
    pls tell me how to read from .properties file.
    waiting eagerly for some reply
    Thanks & Regards,
    HP
    Edited by: user9003827 on Apr 13, 2010 3:42 AM

    I think i have mixed it up :)
    I have looked at it again and believe you are using regular JSP portlets.
    Can you tell what you want to achieve by reading .properties file. Are you meaning the preferences of the portlet or what exactly are you trying to do?
    Reading propertie files is easy:
    // Read properties file.
    Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
        String myKey = properties.getProperty("yourKey");
    } catch (IOException e) {
    }Edited by: Yannick.O on 13-Apr-2010 05:52

  • How to load a Jar file in the class path?

    How to load a Jar file which contains class files, images, etc.. in the classpath without using URLClassLoader.

    You don't "load" jars. If it's on the classpath, you can obtain individual resources from it using various methods on either Class or ClassLoader. Do you mean "how to add a jar to the classpath at runtime"? Can't be done without using a classloader, typically URLClassLoader or a subclass thereof. Why you want to not use the proven method is beyond me. Presumably because you don't understand classloading. In which case, forget it.

  • How to load a client file in a clob using sqlcl

    How to load a client file in a clob using sqlcl

    You don't "load" jars. If it's on the classpath, you can obtain individual resources from it using various methods on either Class or ClassLoader. Do you mean "how to add a jar to the classpath at runtime"? Can't be done without using a classloader, typically URLClassLoader or a subclass thereof. Why you want to not use the proven method is beyond me. Presumably because you don't understand classloading. In which case, forget it.

  • How to load an XML file to oracle9i server?

    I want to use XSU DBMS_XMLsave package to load an XML file to a relational table using PL/SQL from a distant server. Now, I don't know how to load that XML file to the distant server.
    Somebody help me?

    I want to use XSU DBMS_XMLsave package to load an XML file to a relational table using PL/SQL from a distant server. Now, I don't know how to load that XML file to the distant server.
    Somebody help me?

  • "how to load a text file to oracle table"

    hi to all
    can anybody help me "how to load a text file to oracle table", this is first time i am doing, plz give me steps.
    Regards
    MKhaleel

    Usage: SQLLOAD keyword=value [,keyword=value,...]
    Valid Keywords:
    userid -- ORACLE username/password
    control -- Control file name
    log -- Log file name
    bad -- Bad file name
    data -- Data file name
    discard -- Discard file name
    discardmax -- Number of discards to allow (Default all)
    skip -- Number of logical records to skip (Default 0)
    load -- Number of logical records to load (Default all)
    errors -- Number of errors to allow (Default 50)
    rows -- Number of rows in conventional path bind array or between direct path data saves (Default: Conventional path 64, Direct path all)
    bindsize -- Size of conventional path bind array in bytes (Default 256000)
    silent -- Suppress messages during run (header, feedback, errors, discards, partitions)
    direct -- use direct path (Default FALSE)
    parfile -- parameter file: name of file that contains parameter specifications
    parallel -- do parallel load (Default FALSE)
    file -- File to allocate extents from
    skip_unusable_indexes -- disallow/allow unusable indexes or index partitions (Default FALSE)
    skip_index_maintenance -- do not maintain indexes, mark affected indexes as unusable (Default FALSE)
    commit_discontinued -- commit loaded rows when load is discontinued (Default FALSE)
    readsize -- Size of Read buffer (Default 1048576)
    external_table -- use external table for load; NOT_USED, GENERATE_ONLY, EXECUTE
    (Default NOT_USED)
    columnarrayrows -- Number of rows for direct path column array (Default 5000)
    streamsize -- Size of direct path stream buffer in bytes (Default 256000)
    multithreading -- use multithreading in direct path
    resumable -- enable or disable resumable for current session (Default FALSE)
    resumable_name -- text string to help identify resumable statement
    resumable_timeout -- wait time (in seconds) for RESUMABLE (Default 7200)
    PLEASE NOTE: Command-line parameters may be specified either by position or by keywords. An example of the former case is 'sqlldr scott/tiger foo'; an example of the latter is 'sqlldr control=foo userid=scott/tiger'. One may specify parameters by position before but not after parameters specified by keywords. For example, 'sqlldr scott/tiger control=foo logfile=log' is allowed, but 'sqlldr scott/tiger control=foo log' is not, even though the position of the parameter 'log' is correct.
    SQLLDR USERID=GROWSTAR/[email protected] CONTROL=D:\PFS2004.CTL LOG=D:\PFS2004.LOG BAD=D:\PFS2004.BAD DATA=D:\PFS2004.CSV
    SQLLDR USERID=GROWSTAR/[email protected] CONTROL=D:\CLAB2004.CTL LOG=D:\CLAB2004.LOG BAD=D:\CLAB2004.BAD DATA=D:\CLAB2004.CSV
    SQLLDR USERID=GROWSTAR/[email protected] CONTROL=D:\GROW\DEACTIVATESTAFF\DEACTIVATESTAFF.CTL LOG=D:\GROW\DEACTIVATESTAFF\DEACTIVATESTAFF.LOG BAD=D:\GROW\DEACTIVATESTAFF\DEACTIVATESTAFF.BAD DATA=D:\GROW\DEACTIVATESTAFF\DEACTIVATESTAFF.CSV

Maybe you are looking for