Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Accessing Properties File

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Accessing Properties File

    Dear Experts

    I am working on java classes using tomcat, I am able to access the properties file located in the "conf" folder under the webapps directory using this servlet code:

    File propertiesFile = new File(servletContext.getRealPath("./conf/database.properties"));

    but how can i access the same file using java class and not servlet

    Any suggestion will be appreciated


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Accessing Properties File

    Hello there,

    My first thought is that you shouldn't use getRealPath as that wont work that well once you package your application in a war file and deploy it.

    You need to use one of the getResource methods on the class loader.

    public class IOTest {
        public static void main(String[] args) {
            final InputStream inputStream = IOTest4.class.getClassLoader().getResourceAsStream("conf/database.properties");
     
            final Properties properties = new Properties();
            properties.load(inputStream);
        }
    }

    Now that could would try and load conf/database.properties which would be on the classpath. You should make sure you put these resource files in WEB-INF/classes so your file would be in WEB-INF/classes/conf/database.properties

    If you dont put it in classes or WEB-INF folder, you might be exposing the file to the end user as he/she could just browse to http://myserver.domain/mywebapp/conf...ase.properties and see the file.

    Hope this makes any sense.

    // Json

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Re: Accessing Properties File

    Dear Json

    Many thanks for the reply, it was very helpfull

    I need some more help from you in the following:

    I am using the ConnectionPoolManager and for that in the getInstance I need to get the properties file either as a File or String

    conMgr = ConnectionPoolManager.getInstance(propsFile);

    how can I use your provided solution with the above scenario


    Any suggestion will be appreciated

    Thanks in advance

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Accessing Properties File

    I'm not entirely sure what you mean but if you just want to push the properties into the getInstance method could you not just do this?

    public class IOTest {
        public static void main(String[] args) {
            final InputStream inputStream = IOTest4.class.getClassLoader().getResourceAsStream("conf/database.properties");
     
            final Properties properties = new Properties();
            properties.load(inputStream);
     
            conMgr = ConnectionPoolManager.getInstance(properties);
        }
    }


    If not could you tell me what the package name for the ConnectionPoolManager is?

    I use iBATIS myself for database stuff. See iBATIS Home for more information.

    // Json

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Re: Accessing Properties File

    Dear Json,

    Thanks again for the help

    the package I am using is snaq.db.ConnectionPoolManager;
    and the method getInstance dose not support java.util.properties

    Any Suggestion

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Accessing Properties File

    How about trying to use the createInstance method.

    ConnectionPoolManager.createInstance(properties);

    That should work I believe.

    // Json

  7. The Following User Says Thank You to Json For This Useful Post:

    java_mein (May 14th, 2010)

Similar Threads

  1. Vectors - accessing an unknown amount of objects
    By fox in forum Loops & Control Statements
    Replies: 1
    Last Post: May 7th, 2010, 03:54 PM
  2. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 06:11 PM
  3. [URGENT] - Problem accessing web sites with Java!
    By jguilhermemv in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 04:43 PM
  4. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  5. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM