Lab 3 - Properties
Objective
In this lab we will
- create a properties file,
- add some properties to that file,
- load a properties object with the key-value pairs from the file, and
- display those properties to the console.
A new script will be needed to run this lab.
Instructions
- Create a java class named LabThree and place it in the java112.labs2 package.
- Import the java.util package and the java.io package.
- Create a main method that instantiates an instance of the LabThree class and calls the run method. Pass the first argument from the command line to the run() method.
- Add an instance variable of type Properties that is named
properties
. - Take a look at this sample properties file Properties File Example
- Create your own properties file and name it lab2-3.properties. Save it into your projects/config directory.
- Add several entries into the properties file. It doesn’t much matter what they are.
-
Copy this method from here and paste it into your class.
public void loadProperties(String propertiesFilePath) { properties = new Properties(); try { properties.load(this.getClass().getResourceAsStream(propertiesFilePath)); } catch(IOException ioe) { System.out.println("Can't load the properties file"); ioe.printStackTrace(); } catch(Exception e) { System.out.println("Problem: " + e); e.printStackTrace(); } }
-
Create a
run()
method that has one String argument. This method will:- Set the input argument into the propertiesFilePath variable.
- Call the loadProperties() method.
- Using the getProperty() method of the Properties class and System.out.println() output the properties entries to the terminal window.
-
Create a script to run this lab. It will look like this.
#! /bin/sh java -classpath lib/java112Labs.jar:config java112.labs2.$1 $2
-
The new part of the classpath,
lib/java112Labs.jar:config
is so that the properties file can be found. - Assuming you named your new script
runLabsUnit2.sh
, here is how you will run the lab, passing in the properties file:./runLabsUnit2.sh LabThree /lab2-3.properties
Tip
- Use the command
$ ant build_labs
to compile and build the class.
Screenshots
To receive credit for this lab save screenshot(s) in the projects/screenshots/week7
directory.
You may combine the below into one screenshot and name it lab3.png
.
lab3-happyPath.png
: The command line output when running your program's "happy path".lab3-errorHandling.png
: The command line output demonstrating the error handling in your program.
Add, commit, and push and verify all work, including screenshots are visible in GitHub.
Rubric
All of the following must be satisfied to achieve a "Met" status
- Screenshot should clearly show expected output when running the program (happy path).
- Screenshot should clearly show any error conditions handled by the program code.
- Code and screenshots are properly named and saved in the correct directory.
- All lab steps have been accurately and appropriately implemented.
- Code adheres to the course coding standards.
- Commit messages are concise, atomic, and effectively describe the change(s) made.
- JavaDoc documentation is properly implemented, providing clarity and understanding of the code's functionality and usage.
- External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.
Additional Considerations
- Avoid using hard-coded values; utilize parameters passed into the program through the command line.
- Instance variables are private.
- Catch and handle all exceptions.
- Provide friendly and informative messages to users before displaying stack traces for error situations.
- Correct formatting used in properties file.