Week 10: External Libraries and Exceptions
JARs, Maven, OpenCSV, JXMapViewer2 and robust error handling · 22.06.2026
Materials
Download Week 10 Slides (PDF) →
Download the complete Week 10 Java project (ZIP) →
Learning Goals
This week shows how Java programs can use reliable code written by others. By the end, you should be able to:
- explain what a JAR file, classpath, dependency, and Maven coordinate are;
- decide when a problem should be solved with your own code and when a library is appropriate;
- read a real CSV file with OpenCSV instead of a fragile manual parser;
- display geographic points on an OpenStreetMap background with JXMapViewer2;
- separate library-specific code from the application model;
- handle file, CSV, and number-format errors without crashing the whole application.
1. Why External Libraries Matter
Good programmers do not build every technical detail from scratch. A library is reusable code with public classes and methods that your program can call. In this example, we borrow two difficult pieces:
| Library | What it does for us | What our program still owns |
|---|---|---|
OpenCSV |
Reads CSV rows correctly, including quoted commas and escaped quotes. | Turns each valid row into a POI object and skips bad data. |
JXMapViewer2 |
Loads map tiles, converts geographic coordinates to pixels, and handles pan/zoom interaction. | Chooses which POIs to show and how their markers should look. |
Important distinction: OpenStreetMap is the map data source. JXMapViewer2 is the Java Swing component that displays map tiles from that source.
Do not confuse JXMapViewer2 with JMapViewer. This week's project uses JXMapViewer2.
2. Maven Instead of Manual JAR Management
A JAR file is a Java archive containing compiled classes, resources, and metadata. Java can only use those classes if they are on the classpath. Manually downloading JARs works for small examples, but it becomes error-prone when one library depends on several others.
The Week 10 project uses Maven. The important dependencies are declared in pom.xml:
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.9</version>
</dependency>
<dependency>
<groupId>org.jxmapviewer</groupId>
<artifactId>jxmapviewer2</artifactId>
<version>2.8</version>
</dependency>
Maven reads these coordinates, downloads the libraries, downloads their transitive dependencies, and builds the classpath. If an import stays red in IntelliJ, first check whether the project was imported as a Maven project and whether Maven has finished downloading dependencies.
3. The POI Map Project
Week10/
|-- pom.xml
|-- data/
| `-- vienna_pois.csv
|-- lib/
| `-- README.txt
`-- src/at/ac/univie/gis/week10/poimap/
|-- model/
| |-- POI.java
| `-- PoiModel.java
|-- io/
| `-- PoiCsvReader.java
|-- view/
| `-- MapView.java
`-- controller/
`-- MapController.java
The application follows the familiar MVC structure. The data flow is:
data/vienna_pois.csv --OpenCSV--> PoiModel --JXMapViewer2--> map markers
Run: open the Week 10 folder as a Maven project, then execute mvn compile and mvn exec:java.
The map opens in a Swing window. The first run needs an internet connection because OpenStreetMap tiles are downloaded when the map is displayed.
4. Model: Keep Domain Data Plain
The model stores the application's own concepts: points of interest, types, and coordinates. It deliberately does not import OpenCSV or JXMapViewer2. A POI contains plain values:
public class POI {
private final String name;
private final String type;
private final double lat;
private final double lon;
public POI(String name, String type, double lat, double lon) {
this.name = name;
this.type = type;
this.lat = lat;
this.lon = lon;
}
}
This boundary matters. The same model could later be used by a console program, a web service, or a unit test. Only the view needs to know how JXMapViewer2 represents map positions.
5. OpenCSV: Rows Become Objects
CSV looks simple until real text appears in the file. The sample data includes names with commas and quotes:
"Cafe Central, Herrengasse",cafe,48.2106,16.3656
"Restaurant ""Zum Schwarzen Kameel""",restaurant,48.2103,16.3669
Brunnenmarkt,market,not_a_number,16.3380
A manual line.split(",") would break the first row because the comma is part of the name. OpenCSV handles the CSV grammar and returns a clean String[] for each row:
try (CSVReader reader = new CSVReader(
new FileReader(path, StandardCharsets.UTF_8))) {
String[] row;
while ((row = reader.readNext()) != null) {
double lat = Double.parseDouble(row[LAT].trim());
double lon = Double.parseDouble(row[LON].trim());
model.add(new POI(row[NAME], row[TYPE], lat, lon));
}
}
Exceptions in the Reader
The reader uses different exception handling strategies for different problems:
IOException: the file cannot be opened or read. The program reports the problem and starts with an empty model.CsvValidationException: the CSV structure is malformed. The program reports the problem and stops reading.NumberFormatException: one row contains text where a coordinate was expected. The row is skipped, but the rest of the file still loads.
The try-with-resources statement closes the file reader automatically, even if an exception occurs.
6. JXMapViewer2: POIs Become Map Markers
The view converts model data into the library objects needed by JXMapViewer2. Each POI becomes a GeoPosition, then a waypoint, then a marker painted on top of the map tiles.
private PoiWaypoint(POI poi) {
super(new GeoPosition(poi.getLat(), poi.getLon()));
this.poi = poi;
}
The map component also receives standard interaction listeners from the library:
MouseInputListener pan = new PanMouseInputListener(this);
addMouseListener(pan);
addMouseMotionListener(pan);
addMouseWheelListener(new ZoomMouseWheelListenerCenter(this));
The controller adds one application-specific interaction: a combo box filters markers by POI type. The map is still the main view; the control line only changes which model objects are currently displayed.
There is no separate Week 10 assignment. The mandatory SIR cellular automaton assignment from Week 8 has been extended to 31.07.2026 at 23:00 Vienna Time.