- This topic has 8 replies, 3 voices, and was last updated 20 years, 5 months ago by Riyad Kalla.
-
AuthorPosts
-
John FerronMemberI’ve having a little difficulty configuring MyEclipse to handle .properties files in my web application. Right now, I have the property files residing in the src/config directory and I would like MyEclipse to pick up the property file so that I don’t get all of the compiling errors that I’m getting when I reference the global variables. If you have any further questions, feel free to ask. Thanks for your help.
John
Scott AndersonParticipantJohn,
When a project is rebuilt, all Java files in the source tree are compiled and placed in WEB-INF/classes and all .properties files are copied to WEB-INF/classes by the Eclipse build system. So, your properites files should be located at WEB-INF/classes/config/…
Is this not the case?
John FerronMemberWell I moved the properties files to that directory, told MyEclipse to rebuild, and it deleted the property files. What I need to do is to configure MyEclipse to look at the properties files and so i can get rid of the compiling errors for it not knowing the global variables that are in the .properties files.
Riyad KallaMemberJohn,
I think you might have misread what Scott was saying. You don’t want to COPY your properties file into your output directory because Eclipse erases this directory before a rebuild. You want to KEEP your properties file where it was (/src/config).I also don’t understand what you mean by “global variables” in your properties files… properties files are just name/value pairs, like so:
firstName=John lastName=Ferron
and from your Java program you read your properties using a Properties class or a ResourceBundle (or PropertyResourceBundle) class. A popular use for this is internationalizing your interfaces.
If you are getting compiler errors, that has nothing to do with the variables in your properties file because these aren’t variables. You can only define variables in your Java code. Sooooo what I’m getting at is that if you have compiler errors, then you really do have errors, its not some flaky thing that Eclipse is doing.
If you tell us what you are trying to do, and give us an example with some code, we can probably shoot you in the right direction.
John FerronMemberOk.. from your last post. This is what I am coming up with so far.
public void loadProperties(){
try {
Properties p = new Properties();
File file = new File(“global.properties”);
FileInputStream fis = new FileInputStream(file);
p.load(fis);
} catch (IOException ioex) {
// Error reading property file
ioex.printStackTrace();
} //end try catch
} // end method loadPropertiesand this file has the name/value pairs like you had suggested. Now, what is your suggestion for where this file should reside in my directory structre and how is my ant script going to pull in this file for when I am running my ant build script?
Riyad KallaMember😯
I am officially confused now… I don’t know if you are asking how to use properties files, or if you are asking how to use properties files in Ant build scripts or if you are wondering if Smoked or Honey Glazes ham is better….
Can you please clarify your use case, this clearly outside of the scope of MyEclipse and I’m willing to help get you up and running if I can, but I need to know what you are trying to do.
To help this along I need to know:
-
1) What are you trying to do overall? What is the big picture here?
2) What is in your properties files? Names? List of items? What are they going to be used for?
3) Do you need these values in your Java program or just your Ant script?
John FerronMemberOk, I’m really sorry that I am confusing. I have some J2EE development, but not all of this configuring until now. Right now, I am taking some variables out of the java code (like variables used when getting values from resultSets and all information for connecting to the DB) and putting that into the .properties files. I am taking the hard-coded information and storing it in properties files is what I am doing. And my questions are:
1.) How am I suppose to get that properties file loaded into the app??
2.) How do I reference that properties file so that I can get the values out of it that I need?
3.)In my build script, how do I make sure that the properties file gets included into the WAR file.And to answer question three, i need it in the java code. Here is a same of what my .properties file looks like:
DATE_CREATED=DATE_CREATED
END_DATE_TIME=END_DATE_TIME
EXTERNAL_ID=EXTERNAL_ID
JOB_NUMBER=JOB_NUMBER
INIT_CONTEXT=java:comp/env
DATA_CONTEXT=jdbcInterface
URL = etc…..I hope that I am more clearer and less confusing.
John FerronMemberI figured out how to load the properties file.. it took a while.. but i got it working with a ResourceBundle.
Now my only question is for a properties file, do you need anything to demark the start or end of the file. The reason why i ask is that the last field in my properties file is not being picked up. Thanks.
John
Riyad KallaMemberI am taking the hard-coded information and storing it in properties files is what I am doing.
OHhhh ok I get it now.
1.) How am I suppose to get that properties file loaded into the app??
Ok yes you can use a ResourceBundle OR you can use a Properties object (from java.util). In your case its probably more correct to use a Properties file, where as ResourceBundles and PropertyResourceBundles are typically used for internationalization of interfaces (e.g. “Use this file if English is the locale, or use this file if its French, etc. etc.”).
Also, you should stick your properties file somewhere in your source tree that makes sense. Then you can use this to load it:
Properties properties = new Properties(); try { properties.load(ClassLoader.getSystemResourceAsStream("com/test/test.properties")); } catch (IOException e1) { e1.printStackTrace(); }
Change the package path and name of properties file to suite your needs though.
2.) How do I reference that properties file so that I can get the values out of it that I need?
Look at my previous code snippet, now your “properties” object can be used like this:
String dateCreated = properties.getProperty("DATE_CREATED");
3.) In my build script, how do I make sure that the properties file gets included into the WAR file.
Just make sure to add a copy command that filters on *.properties files, OR just copies the file directly into your build dir, then when you run your WAR task, it will zip everything up nicely. For example, I’ve got a few JAR tasks in a Ant script for another project, and I’ve got 4 or 5 properties files that won’t get copied unless I explicitly add copy commands for them to get over into the build dir before I make the JAR files.
I hope that I am more clearer and less confusing.
Yes, its very clear now. Thanks for the details. I hope this helps.
-
AuthorPosts