|
| 1 | +# Using JDeps |
| 2 | + |
| 3 | +In this exercise, we will using the `jdeps` tool to resolve the modules we need to include in a runtime. |
| 4 | + |
| 5 | +## The Program |
| 6 | + |
| 7 | +This program is designed to call a url endpoint and print to the JDK logger the response it receives. |
| 8 | + |
| 9 | +```java |
| 10 | +package foo; |
| 11 | + |
| 12 | +import java.net.URI; |
| 13 | +import java.net.http.HttpClient; |
| 14 | +import java.net.http.HttpRequest; |
| 15 | +import java.net.http.HttpResponse; |
| 16 | +import java.net.http.HttpResponse.BodyHandlers; |
| 17 | +import java.time.Duration; |
| 18 | +import java.util.logging.Level; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +public class GetWebsite { |
| 22 | + |
| 23 | + private static final Logger LOGGER = Logger.getLogger(GetWebsite.class.getName()); |
| 24 | + public static void main(String... args) throws InterruptedException { |
| 25 | + String url = args[0]; |
| 26 | + try (HttpClient client = HttpClient.newHttpClient();) { |
| 27 | + LOGGER.log(Level.INFO, "Calling: " + url); |
| 28 | + HttpRequest request = HttpRequest.newBuilder(URI.create(url)).GET().build(); |
| 29 | + client.sendAsync(request, BodyHandlers.ofString()) |
| 30 | + .thenApply(HttpResponse::body).thenAccept(r -> LOGGER.log(Level.INFO, r)); |
| 31 | + client.awaitTermination(Duration.ofMillis(1000L)); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +## Resolving Module Dependencies with JDeps |
| 38 | +Attempting to run `GetWebsite` with the runtime we created in the previous exercise will fail, throwing `ClassNotFound` exceptions. This is because some of the classes referenced in `GetWebsite` are located outside the `java.base` module. |
| 39 | + |
| 40 | +The `jdeps` tool can be used to find which modules `GetWebsite` depends upon, which can be done with: |
| 41 | + |
| 42 | +``` |
| 43 | +$jdeps GetWebsite.class |
| 44 | +``` |
| 45 | + |
| 46 | +Which will return this table: |
| 47 | + |
| 48 | +``` |
| 49 | +jdeps GetWebsite.class |
| 50 | +GetWebsite.class -> java.base |
| 51 | +GetWebsite.class -> java.logging |
| 52 | +GetWebsite.class -> java.net.http |
| 53 | + foo -> java.lang java.base |
| 54 | + foo -> java.lang.invoke java.base |
| 55 | + foo -> java.net java.base |
| 56 | + foo -> java.net.http java.net.http |
| 57 | + foo -> java.time java.base |
| 58 | + foo -> java.util.concurrent java.base |
| 59 | + foo -> java.util.function java.base |
| 60 | + foo -> java.util.logging java.logging |
| 61 | +``` |
| 62 | + |
| 63 | +The top of the table describes the modules the class depends on, the bottom portion shows how those values were calculated. |
| 64 | + |
| 65 | +JDeps can also be configured to provide a more user friendly output with the option `--print-module-deps` which will return a comma-separated list of the modules: |
| 66 | + |
| 67 | +``` |
| 68 | +$ jdeps --print-module-deps GetWebsite.class |
| 69 | +java.base,java.logging,java.net.http |
| 70 | +``` |
| 71 | + |
| 72 | +This can then be copied into jlink to build runtime that can run `GetWebsite` like in this command: |
| 73 | + |
| 74 | +``` |
| 75 | +$ jlink --add-modules java.base,java.logging,java.net.http --output get-website-image |
| 76 | +``` |
| 77 | + |
| 78 | +This runtime can be used to successfully execute `GetWebsite` |
| 79 | + |
| 80 | +``` |
| 81 | +$ ./get-website-image/bin/java GetWebsite http://localhost:8000 |
| 82 | +``` |
| 83 | + |
| 84 | +[In the next exercise](../step-3-adding-non-jdk-modules) we will look at how to add non-JDK modules to a runtime image created with JLink. |
0 commit comments