0

I need a configuration file in my program to store some information, I saw some examples of properties files and was trying to use them but whenever I try this second line on NetBeans i get 'Package sortConfig doesn't exist' '<identifier> Expected' and 'Illegal start of type'.

The problem is I already saw some 10 examples all doing this in the same way, and I have no clue to what's going on.

Properties sortConfig = new Properties();

sortConfig.load(this.getClass().getClassLoader().getResourceAsStream("sortConfig.properties"));

Any help would be appreciated

my .java classes and my properties file are on the src, inside the same package folder

2
  • Where is sortConfig.properties located? Can you please add your package structure? Commented Jan 23, 2011 at 23:24
  • 1
    You seem to be getting a compile error, but it doesn't make sense. The two lines compile fine, they must be misplaced or whatever. Provide more context. Commented Jan 23, 2011 at 23:41

2 Answers 2

1

It looks like you do not have the fully qualified path to your properties file. You can get to it in 2 ways:-

  1. Using java.util.ResourceBundle:

ResourceBundle bundle =ResourceBundle.getBundle("br.com.example.sortConfig"); //.properties is implied

or

  1. Using ClassLoader.getResouceAsStream:
sortConfig.load(this.getClass().getClassLoader().getResourceAsStream("br/com/example/sortConfig.propertie"));

For a good tutorial on how to load properties files resources check out this link.

Sign up to request clarification or add additional context in comments.

1 Comment

the ClassLoader didn't work, but the ResourceBundle worked beautifully so I didn't bother, Thanks for the Answer
0

An alternative could be to use this.getClass().getResourceAsStream() which accepts relative pathnames (relative to the package your class is in, that is), so you could simply write

sortConfig.load(this.getClass().getResourceAsStream("sortConfig.properties"));

This is useful when you specifically want to rely on your class and properties file being in the same package. (So when you move one during a refactoring, you'll have to move the other too.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.