Load Properties Files

Hi,
I try to load the configProperties.load(new FileInputStream("properties/log4j.properties")), but it always didn't see the property file.
I had to hard code like configProperties.load(new FileInputStream("C:\\Java\\WebTool\\properties\\log4j.properties")) for getting the properties.
Did anyone know how to get the properties is working on local and unix box too ?
Thanks.

Reading from a property file and using
System.getProperty are two distinct things!
To open a file that comes with your application you
should do something like this:
InputStream in =
obj.getClass().getResourceAsStream("/properties/log4j.
properties");The part where you bang your head on the keyboard is
where you try to compose the right path to pass to
getResourceAsStream.Oh, absolutely, DrJam (you might not ever live that one down, you know ;o) ... anyway, it was this part ["but it always didn't see the property file."], that I was picking up on ...
The OP went on to point out that giving it the whole path seemed to work. My post was not as clear as it should have been, but I was proposing that he/she actually put the file in his/her user.home; then do something like:
private static final String USER_HOME = System.getProperty("user.home");
InputStream in = obj.getClass().getResourceAsStream(USER_HOME+"log4j.properties");

Similar Messages

  • WSAD : loading properties file from EBJ project

    Hi,
    I am trying to load properties file contains database parameters into my EJB project. I am using WSAD as IDE tool
    can you please help me with following.
    1. where to place the .properties file physically ?
    2.how to load the file to my EJB/DAO classes
    3.can i use the same location to place log4j.properties file as well ?
    I have tried doing in different ways but none is working.
    thank you
    Narendra

    got it working , for information pl go to the following page and section "Using log4j in an EJB Application"
    i have followd same steps for loading my properties file .
    http://sys-con.com/story/?storyid=43413&DE=1

  • Exchange profile error: dbconnect.prop: failed to load properties file .\dbconnect.properties

    All,
    We are using PO 7.4 with SP5, and we have an issue while accessing URL Exchange profile page (http://<host:port>/webdynpro/dispatcher/sap.com/com.sap.xi.exprofui/XIProfileApp) it throw's the below error.
    dbconnect.prop: failed to load properties file .\dbconnect.properties
    dbconnect.prop: failed to load properties file
    Did some one seen this before and have resolved ? Any help is highly appreciated.
    Thank you, Pranay

    Though below note is not applicable to you, i would suggest you to check if the destination is created by the configuration wizard? If not you can create one and see if it helps.
    Create Java RFC destination XI_EXCHANGE_PROFILE manually: Navigate to http://[host]:[port]/nwa --> Configuration --> Infrastructure --> Destinations. Create the RFC Destination with PILDUSER as logon user.
    1525193 - PI CTC: Exchange Profile Access after (EhP) Upgrade

  • Fail to load properties file

    Hi
    I can't load properties file. Please help!
    my setting is listed as follows:
    My properties file "kpggc.properties" is stored in the path : W:\kpggc\KPGGCWorkspace\KPGGCProject\src\hkpost\kpggc\client\resources
    I have already add the above path into the classpath and my application is a standalone application. I use the following codes to load the "kpggc.properties" file:
    private static boolean getProperties()
    Properties properties = new Properties();
    try
    properties.load(new FileInputStream("kpggc.properties"));
    catch (IOException e)
    System.out.println("Cannot find the properties file 'kpggc.properties'");
    catch (Exception e)
    System.out.println(e.getMessage());
    After running the above method, error message "Cannot find the properties file 'kpggc.properties'""always show.
    Thanks in advance.

    there is a way. u need not give the complete path of ur file.
    include the path of the properties file in ur classpath.
    in ur code, do the following:
    (inputStream and properties are declared before this code.)
    inputStream = ClassLoader.getSystemResourceAsStream(fileName);
    properties.load(inputStream);
    regards

  • Unable to load properties file from filesystem

    Hi,
    I have a client swing-applicatiion packaged in a jar-file. The application uses some resource files, including a xxxx.properties file to set defaults for the application. All resource files are included in the jar file and everything works as expected. I load the xxxx.properties file as follows:
    is_ = getClass().getResourceAsStream("/resources/xxxx.properties");
    Because it is not possibl to change any property values inside the jar-file without recreating this jar-file, i want to exclude the xxxx.properties file from the jar-file and put it in the folder containing the jar-file.
    So i have:
    drive:/folder/app.jar
    drive:/folder/xxxx.properties
    Now i read the xxxx.properties file from disk in stead of the 'resouce'-folder within my application (jar):
    is_ = getClass().getResourceAsStream("drive:/folder/xxxx.propoerties");
    but it doesn't work, the file isn't found.
    What am i missing here?
    Any suggestions?

    The best way to approach this is to include the properties file in the Jar as you originally did. If the user changes any property, save the entire set of properties to a sub-directory of user.home (perhaps based on the package name of the main class). Check for the copy in user.home before using the 'default' properties built into the Jar.
    Other places/ways to store the changed information.
    - java.util.prefs.Preferences
    - Assuming the app. has a GUI and can be deployed over a network, the JNLP API PersistenceService. Here is a demo. of the PersistenceService (http://pscode.org/jws/api.html#ps).
    Edit 1:
    I did not at first notice that this post was in Java Applet Development.
    <li> Applets can also (theoretically) use cookies to store information. This is prone to failure when users disable cookies.
    <li> To store information on the local file system (e.g. in user.home) or to use Preferences (AFAIU) would require trusted code.
    <li> If the user has the Next Generation Java Plug-In (Java 1.6.0_10+) it is possible to leverage the JNLP API services in an embedded & sand-boxed applet.
    Edited by: Andrew Thompson on Feb 1, 2011 9:32 AM

  • Webservice - loading properties file

    Hi,
    I'm creating a webservice which connects to server A, sends a message M and gets a response message R back. The values to connect to server A are in a properties file which I need to load in the webservice. Since I connect to the same server everytime, (i just send diff messages and get appropriate response back) I just want to load the properties file just once. I don't want to load the properties file everytime the webservice gets called so I can improve the performance. Can you please tell me how I would be able to achieve this? I'm planning to deploy this webservice on to a tomcat server.

    If you enable CDI in your tomcat then you can use an @ApplicationScoped bean to hold the values.
    Another option is to use a caching library like ehcache.

  • 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

  • Specifying a path to loada properties file

    I'm trying to load a properties file in java, the properties file is located in
    META-INF/sample.properties
    the class that loads the properties is in
    WEB-INF/classes/com/pkg1/pkg2/pkg3/pkg4/samplepgm.java
    how do I specify the path to load the properties file...

    you can only add images by using the place-function
    so you need to address the rectangle where the image is placed in.
    you can use use scriptlabels and then you can use something in the terms of
    document.Rectangles.Item("image").place(linktoyourfile)

  • Alternatives to loading .properties files for every request

    I am just starting for a company, and they loading a bunch of .properties files for every request. I've told them that according to JEE specs, using the file system like that is a violation, as it's not scalable. I've suggested the following alternatives:
    1. Load a file once, in a static initializer, or something to that effect. Re-start the app if a change needs to be made in properties.
    +: few changes
    -: requires a new war/ear to make a change
    2. Put properties that are truly variable in a database record.
    -: requires DB schema change.
    3. For instance, i've though about using something like jconsole to change an mbean property, and then web apps using that property.
    4. Define a single web app that exposes an EJB/JMS/RMI object which serves properties to other web apps. If a change is needed, only 1 web app needs to be re-started, although it would still have to be re-packaged. Perhaps this app could load this stuff from the DB.
    5. Have a properties service, and expose it as an RMI object. If a change needs to be made on the fly, use rmi to make the updates.
    I'm leaning towards a combination of 1, 5, and possibly 4.
    I would like to know what other alternatives might be available.
    Thanks.

    I am just starting for a company, and they loading a
    bunch of .properties files for every request. I've
    told them that according to JEE specs, using the file
    system like that is a violation, as it's not
    scalable. well, it's just boneheaded that's all
    I've suggested the following alternatives:
    1. Load a file once, in a static initializer, or
    something to that effect. Re-start the app if a
    change needs to be made in properties.that's what most people do
    2. Put properties that are truly variable in a
    database record.well, databases don't normally hold config info, but you can do what you want
    -: requires DB schema change.
    3. For instance, i've though about using something
    like jconsole to change an mbean property, and then
    web apps using that property.
    4. Define a single web app that exposes an
    EJB/JMS/RMI object which serves properties to other
    web apps. If a change is needed, only 1 web app needs
    to be re-started, although it would still have to be
    re-packaged. Perhaps this app could load this stuff
    from the DB.doing EJB or RMI for getting properties sounds like overkill in the extreme to me

  • How can load properties file for one time and use in entire application

    Hi folks,
    I dont want to read properties file every time , and want to get property value in any servelet or jsp page.(means i want to read only property file only for once and want to get value in any where ) , how to do this.
    Message was edited by:
    RajeshwarReddyT

    means we have to read file every time Know but i dont
    want to be happen that ??? No you don't . You read the file once. You store it in the hashmap. Then you hand that hashmap to whatever class needing the data.
    getProperties() returns the hashmap, doesn't read the file.
    Maybe I should have called the method getMap or something.
    Message was edited by:
    karma-9

  • Home made classLoader not being able to load properties files

    Well, I need to use my own class loader to load some class dynamically. My own class loader works wonder... except when some library that I use try to load their property files from their jar library.
    My class loader overrides findClass and receives the classes (or files) names of the binaries to load in the following format: net.sourceforge.jtds.jdbc.Messages
    It loads fine the class Messages.class but it seems like the library I use also wants to load Messages.properties and fails doing so with the following message
    Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No message resource found for message property prop.servertype
         at net.sourceforge.jtds.jdbc.Messages.get(Messages.java:120)
         at net.sourceforge.jtds.jdbc.Messages.get(Messages.java:67)
         at net.sourceforge.jtds.jdbc.Driver.parseURL(Driver.java:375)
         at net.sourceforge.jtds.jdbc.Driver.setupConnectProperties(Driver.java:240)
         at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:179)
         at java.sql.DriverManager.getConnection(Unknown Source)
    I know it tried to load a property file because my class loader receives calls to findClass with the following parameter:
    - once with: net.sourceforge.jtds.jdbc.Messages_en_US
    - and once with: net.sourceforge.jtds.jdbc.Messages_en
    which are the way Java.util.RessourceBundle loads its property files depending of the Localisation setting of your computer. Plus, I have verifyed the jar source and it is exactly what it is doing.
    When I use the default class loader, it works fine.
    How can I build my own class loader in order that the RessourceBundle loader also works???
    Please! If you know anything about it let me know because I've been on the subject for a while now and so far, no luck!
    Thanks

    I have found the answer... as described in:
    http://java.sun.com/javase/6/docs/technotes/guides/lang/resources.html
    the problem was that the properties are loaded with the getResource & getResourceAsStream methods and I didn't knew that one. I thought that is was loaded through findClass because I saw the property files trying to be loaded through findClass.
    The truth is that it tries to load with the getRessources methods and if it fails tries with the findClass/loadClass.
    To Fix the problem, I have simply overriden the getRessourceAsStream to do my magic and that was it.
    Thanks

  • Location of application-specific properties files

    I am using Tomcat 4.0.3 under WinXP Pro, and I am trying to get my
    application to "find" an application-specific properties file. I am having problems trying to determine which directory to put this file in.
    Here is the Java code I am using for this class:
    import java.util.Properties;
    import java.io.FileInputStream;
    public class ForumProperties extends Properties {
       private static final String DEFAULT_FILENAME = "/WEB-INF/classes/mystuff.properties";
       private static ForumProperties globalProps;
       private ForumProperties() {
       private ForumProperties(String fileName) throws Exception {
          this();
          load(new FileInputStream(fileName));
       public static ForumProperties getInstance() throws Exception {
          try {
             if (globalProps == null)
                globalProps = new ForumProperties(DEFAULT_FILENAME);
          } catch (Exception ex) {
             ex.printStackTrace(System.out);
             throw new Exception("Error loading properties file");
          return globalProps;
    }I have put the properties file in just about every directory I can think of to try and find out where it's supposed to be, but I can't get it to work. I can put the file into the "user.home" System property (which turns out to be C:\Documents and Settings\Administrator) and have it pick it up, but I would prefer to put the file into the path of the application.
    I have tried four different methods to load this file:
    1. load(new FileInputStream(fileName)); (same as above)
    2. load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
    3. load(this.getClassLoader().getResourceAsStream(fileName));
    4. load(getServletContext().getResourceAsStream(fileName));
    All of them fail for not finding the file except #4, but that's not the
    optimal solution as it requires a servlet context, which for "genericness of code reasons", I DON'T want to do.
    As an aside, I pulled the "java.class.path" from the System properties to see where it's pointing, and all it has in it is
    "<CATALINA_HOME>\bin\bootstrap.jar" with no other paths included.
    Is there an attribute or something in the server.xml or web.xml file I am forgetting to set, or another approach I should be taking?
    Bob

    See if this code fragment helps.
              String MAIN_FILE_PATH = "./BlueGnome/demos/Properties/DemoPropertiesFile.properties";
              java.net.URL url = ClassLoader.getSystemClassLoader().getResource(MAIN_FILE_PATH);
              System.out.println("PATH: resolved name = " + url);
              // Get the file.
              // We could just use FileInputStream here.  Then we would have
              // to provide a path that was meaningful to the system like
              //     c:\java\BlueGnome\demos\Properties\DemoPropertiesFile.properties
              // Doing it this way allows us to use generic directory
              // seperators and allows for it to search for the file using
              // the CLASSPATH.  That also allows for it to be in a jar file.
              java.io.InputStream is;
              is = ClassLoader.getSystemClassLoader().getResourceAsStream(MAIN_FILE_PATH);
              if (is == null)
                   throw new Exception("Resource File "
                        + MAIN_FILE_PATH + " not found.");
              aProperties.load(is);

  • Loading properties in JAR directory

    I am trying to load properties files that are in the directory structure of the JAR file. The first properties object loads, but subsequent properties objects will not load. The code is as follows:
    public Properties loadProperties(String file_name)
    InputStream data = null;
    Properties props = null;
    try
    data = getClass().getResourceAsStream(file_name);
    //if(data==null) exit("ERROR: "+file_name);
    props = new Properties();
    props.load(data);
    data.close();
    data = null;
    catch(Exception e)
    handleException(e, file_name);
    if(DEBUG)
    printline("Property Loaded Successfully: " + file_name);
    printline(props.toString());
    return props;
    the file names have the following setup
    /directory/subdir1/subdir2/subdir3/file_name
    The code works fine when not JAR'd - any suggestions?

    If your program is in an executeable jar file then add the following in the manifest
    class-path: junk.jar
    junk.jar has
    pjb/props/myconfig.properties file
    To access the properties in junk from your executable jar file in your class, do the following
    Locale eng = Locale.ENGLISH;
    String myvalue  = java.util.ResourceBundle.getBundle("pjb.props.myconfig",eng).getString("greeting");That should work
    Or if you not using an executable jar file just set your CLASSPATH to the directory where that junk.jar is.
    java -cp c:\myjavalib\stash\                 <-----Assmuing junk.jar is in thereJava dynamically reads jar files.

  • 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

Maybe you are looking for