Results 1 to 13 of 13
  1. #1
    Happy9959 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default Not able to load a properties file

    Hi all,

    when i am trying to load the properties file in to my class, my program is not able to identify the properties file, i even tryed by passing the entire path but there is no luck. Please find the code that i am facing problem

    Code:

    public static String mymethod(String ss)
    {
    ResourceBundle resources = ResourceBundle.getBundle(ss);
    String s = resources.getString("USER");
    System.out.println(s);
    return s;
    }

    public static void main(String[] args) {
    String st =mymethod("DBase");
    System.out.println(st);
    }

    The Exception i am getting is :

    java.util.MissingResourceException: Can't find bundle for base name DBase, locale en_US
    at java.util.ResourceBundle.throwMissingResourceExcep tion(Unknown Source)
    at java.util.ResourceBundle.getBundleImpl(Unknown Source)
    at java.util.ResourceBundle.getBundle(Unknown Source)
    at dev_java.examples.HelloWorld.mymethod(HelloWorld.j ava:13)
    at dev_java.examples.HelloWorld.main(HelloWorld.java: 25)

    ---------------- Exception Ends ------------

    Any help can be appreciated

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Hi try with "Locale" class as in the following sample:

    Java Code:
    import java.util.*;
    
    public class Sample1
    {
    	public static void main(String[] args)
    	{
    		String language;
    		String country;
    
    		Locale locale;
    		ResourceBundle rb;
    
    		if (args.length != 2)
    		{
    			language = new String("en");
    			country = new String("US");
    		}
    		else
    		{
    			language = new String(args[0]);
    			country = new String(args[1]);
    		}
    
    		locale = new Locale(language, country);
    		rb = ResourceBundle.getBundle("MessagesBundle", locale);
    		
    		System.out.println(rb.getString("welcome"));
    		System.out.println(rb.getString("sayThanks"));
    	}
    }
    Last edited by baskar.nitt; 07-28-2008 at 09:01 AM. Reason: Duplicate content

  4. #4
    Happy9959 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    thanks for your prompt reply,
    I think it is not the problem of the syntax, but it is not able to locate the Properties file(even after using Local).

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Shall we know for what purpose you are loading properties file. If the purpose is for getting database details(user name,pwd, database) means use the following code. Just use .config file instead of properties file.
    The "db.config" file should be in the current directory.


    Java Code:
    Properties p = new Properties();
    p.load(new FileInputStream(new File("db.config")));
    
    		
    String dbvar  = p.getProperty("USER");
    Java Code:
    db.config:
    USER=testuser
    PWD=testpwd
    Last edited by baskar.nitt; 07-28-2008 at 10:25 AM.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Seems to me our thread stater stuck with some issues on the file URL. Debug and see, you find the correct way.

  8. #8
    Happy9959 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Seems to me our thread stater stuck with some issues on the file URL. Debug and see, you find the correct way.
    Hi all thanks for you support,

    Eranga: Thanks for you valueable information, as i dont have much idea on URL and ClassLoader classes, i achieved it using properties any way i will try with URl and ClassLoader aslo and will let u know if i get strucked

    Baskar: Thanks for your replys and i resolved my issue.

    Looking forward for both of ur support in future.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Nice to help you pal. :)

    Check more about ClassLoader and stuff. It's much easy actually. Just Google it, you can find lots of examples related to it.

  10. #10
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    Nice to help you...

  11. #11
    jack239 is offline Member
    Join Date
    Jul 2008
    Posts
    35
    Rep Power
    0

    Default

    I didn't understand why ResourceBundle is not loading the properties file? I've used in my java project and it worked-

    private static final String BUNDLE_NAME =
    "com.myproject.services.common.ServicesResourc es";
    private static final ResourceBundle RESOURCE_BUNDLE =
    ResourceBundle.getBundle(BUNDLE_NAME);
    New to Java/PHP/Javascript development?
    For free help go to- www.techcubetalk.com

  12. #12
    Happy9959 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by jack239 View Post
    I didn't understand why ResourceBundle is not loading the properties file? I've used in my java project and it worked-

    private static final String BUNDLE_NAME =
    "com.myproject.services.common.ServicesResourc es";
    private static final ResourceBundle RESOURCE_BUNDLE =
    ResourceBundle.getBundle(BUNDLE_NAME);
    Hi Jack,
    you are write, i have tried with it again and now i can read the file using ResourceBundle class also, the Mistake which i commited is i havent specified the complete path of the properties file.The below is the corrected code
    Code:

    try{
    //String path = "C:\\dev_java\examples\Java_Dev\dev_java\examples\ DBase.properties";
    String path = "C://dev_java/examples/Java_Dev/dev_java/examples/DBase.properties";
    Properties p = new Properties();
    p.load(new FileInputStream(new File(path)));

    String dbvar = p.getProperty("USER");
    System.out.println(dbvar);

    } catch (Exception e){
    System.out.println("FILE_NOT_FOUND_EXCEPTION");
    }

    Sorry for confusing you

  13. #13
    jack239 is offline Member
    Join Date
    Jul 2008
    Posts
    35
    Rep Power
    0

    Default

    Thanks, now the stuff is clear to me
    New to Java/PHP/Javascript development?
    For free help go to- www.techcubetalk.com

Similar Threads

  1. How to Save/Load Vector to/from file
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:37 PM
  2. how to load a file in remote machine
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 10:33 PM
  3. How to load binary content of a .class file
    By boy22 in forum Advanced Java
    Replies: 1
    Last Post: 08-03-2007, 07:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •