Java 11 Quickstart¶
Welcome¶
GeoTools 21.0 and newer can be used in a Java 11 modular application. This requires a few additions on top of the regular tutorial application.
This quickstart assumes you have already run through one of the regular quickstarts:
Java Install¶
Instead of Java 8, we will want to install Java 11.
Download the latest Java 11 JDK:
OpenJDK: http://jdk.java.net/11/
At the time of writing the latest Java 11 release was:
jdk-11.0.1
Click through the installer you will need to set an acceptance a license agreement and so forth. By default this will install to:
C:\Program Files\Javajdk-11.0.1
Update the
JAVA_HOME
system variable so that maven uses the newly installed Java 11:JAVA_HOME =
C:\Program Files\Java\jdk-11.0.1
Verify
mvn
is using the correct Java version by running:mvn -version
It should report a Java version of 11.0.1.
Note
In this tutorial we refer to file and directory paths as used by Windows. If you are fortunate enough to be using another operating system such as Linux or OSX all of the commands and source code below will work, just modify the paths to suit.
Updating the POM¶
A few changes are required to compile and run on Java 11:
Add dependencies for gt-render and gt-main, since we import packages from them:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swing</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-render</artifactId> <version>${geotools.version}</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>${geotools.version}</version> </dependency> </dependencies>
Update the Maven plugin versions. We want to ensure we use the most recent, Java 11-compatible, versions. We will also update the configuration of the Maven compile plugin to target Java 11:
<build> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <release>11</release> </configuration> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>3.0.0-M1</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>3.0.0-M1</version> </plugin> </plugins> </pluginManagement> </build>
Adding Module Info¶
Java 9 introduced the concept of modules to the JVM.
A module is a named, self-contained bundle of Java code. Modules may depend upon other modules, and may export packages from themselves to other modules that depend upon them. Module configuration is controlled by the module-info.java
file.
For a more detailed overview of the module system, refer to the State of the Module System.
Add a
module-info.java
file undertutorialsrcmainjava
.Name our module
org.geotools.tutorial.quickstart
:module org.geotools.tutorial.quickstart { }
Then, add the modules we depend upon:
module org.geotools.tutorial.quickstart { requires java.desktop; requires org.geotools.main; requires org.geotools.shapefile; requires org.geotools.swing; requires org.geotools.render; }
You’ll notice the four GeoTools modules match those added as dependencies in the
pom.xml
. We also include thejava.desktop
module, which contains user interface components required for the app to function.
Running the application¶
If you need some shapefiles to work with you will find a selection of data at the http://www.naturalearthdata.com/ project which is supported by the North American Cartographic Information Society. Head to the link below and download some cultural vectors. You can use the ‘Download all 50m cultural themes’ at top.
Unzip the above data into a location you can find easily such as the desktop.
You can run the application using Maven on the command line:
mvn exec:java -Dexec.mainClass=org.geotools.tutorial.quickstart.Quickstart
The application will connect to your shapefile, produce a map context, and display the shapefile.
A couple of things to note about the code example:
The shapefile is not loaded into memory. Instead it is read from disk each and every time it is needed. This approach allows you to work with data sets larger than available memory.
We are using a very basic display style here that just shows feature outlines. In the examples that follow we will see how to specify more sophisticated styles.
Things to Try¶
Try out the different sample data sets.
Try adding a different profiles to your
pom.xml
for running on Java 8 and Java 11Hint
You’ll want to exclude the module-info,java file from the build when running on Java 8.
Advanced: Try to get another tutorial to run on Java 11 using the module system.