$npx -y skills add decebals/claude-code-java --skill java-migrationGuide for upgrading Java projects between major versions (8→11→17→21→25). Use when user says "upgrade Java", "migrate to Java 25", "update Java version", or when modernizing legacy projects.
| 1 | # Java Migration Skill |
| 2 | |
| 3 | Step-by-step guide for upgrading Java projects between major versions. |
| 4 | |
| 5 | ## When to Use |
| 6 | - User says "upgrade to Java 25" / "migrate from Java 8" / "update Java version" |
| 7 | - Modernizing legacy projects |
| 8 | - Spring Boot 2.x → 3.x → 4.x migration |
| 9 | - Preparing for LTS version adoption |
| 10 | |
| 11 | ## Migration Paths |
| 12 | |
| 13 | ``` |
| 14 | Java 8 (LTS) → Java 11 (LTS) → Java 17 (LTS) → Java 21 (LTS) → Java 25 (LTS) |
| 15 | │ │ │ │ │ |
| 16 | └──────────────┴───────────────┴──────────────┴───────────────┘ |
| 17 | Always migrate LTS → LTS |
| 18 | ``` |
| 19 | |
| 20 | --- |
| 21 | |
| 22 | ## Quick Reference: What Breaks |
| 23 | |
| 24 | | From → To | Major Breaking Changes | |
| 25 | |-----------|------------------------| |
| 26 | | 8 → 11 | Removed `javax.xml.bind`, module system, internal APIs | |
| 27 | | 11 → 17 | Sealed classes (preview→final), strong encapsulation | |
| 28 | | 17 → 21 | Pattern matching changes, `finalize()` deprecated for removal | |
| 29 | | 21 → 25 | Security Manager removed, Unsafe methods removed, 32-bit dropped | |
| 30 | |
| 31 | --- |
| 32 | |
| 33 | ## Migration Workflow |
| 34 | |
| 35 | ### Step 1: Assess Current State |
| 36 | |
| 37 | ```bash |
| 38 | # Check current Java version |
| 39 | java -version |
| 40 | |
| 41 | # Check compiler target in Maven |
| 42 | grep -r "maven.compiler" pom.xml |
| 43 | |
| 44 | # Find usage of removed APIs |
| 45 | grep -r "sun\." --include="*.java" src/ |
| 46 | grep -r "javax\.xml\.bind" --include="*.java" src/ |
| 47 | ``` |
| 48 | |
| 49 | ### Step 2: Update Build Configuration |
| 50 | |
| 51 | **Maven:** |
| 52 | ```xml |
| 53 | <properties> |
| 54 | <java.version>21</java.version> |
| 55 | <maven.compiler.source>${java.version}</maven.compiler.source> |
| 56 | <maven.compiler.target>${java.version}</maven.compiler.target> |
| 57 | </properties> |
| 58 | |
| 59 | <!-- Or with compiler plugin --> |
| 60 | <plugin> |
| 61 | <groupId>org.apache.maven.plugins</groupId> |
| 62 | <artifactId>maven-compiler-plugin</artifactId> |
| 63 | <version>3.12.1</version> |
| 64 | <configuration> |
| 65 | <release>21</release> |
| 66 | </configuration> |
| 67 | </plugin> |
| 68 | ``` |
| 69 | |
| 70 | **Gradle:** |
| 71 | ```groovy |
| 72 | java { |
| 73 | toolchain { |
| 74 | languageVersion = JavaLanguageVersion.of(21) |
| 75 | } |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Step 3: Fix Compilation Errors |
| 80 | |
| 81 | Run compile and fix errors iteratively: |
| 82 | ```bash |
| 83 | mvn clean compile 2>&1 | head -50 |
| 84 | ``` |
| 85 | |
| 86 | ### Step 4: Run Tests |
| 87 | |
| 88 | ```bash |
| 89 | mvn test |
| 90 | ``` |
| 91 | |
| 92 | ### Step 5: Check Runtime Warnings |
| 93 | |
| 94 | ```bash |
| 95 | # Run with illegal-access warnings |
| 96 | java --illegal-access=warn -jar app.jar |
| 97 | ``` |
| 98 | |
| 99 | --- |
| 100 | |
| 101 | ## Java 8 → 11 Migration |
| 102 | |
| 103 | ### Removed APIs |
| 104 | |
| 105 | | Removed | Replacement | |
| 106 | |---------|-------------| |
| 107 | | `javax.xml.bind` (JAXB) | Add dependency: `jakarta.xml.bind-api` + `jaxb-runtime` | |
| 108 | | `javax.activation` | Add dependency: `jakarta.activation-api` | |
| 109 | | `javax.annotation` | Add dependency: `jakarta.annotation-api` | |
| 110 | | `java.corba` | No replacement (rarely used) | |
| 111 | | `java.transaction` | Add dependency: `jakarta.transaction-api` | |
| 112 | | `sun.misc.Base64*` | Use `java.util.Base64` | |
| 113 | | `sun.misc.Unsafe` (partially) | Use `VarHandle` where possible | |
| 114 | |
| 115 | ### Add Missing Dependencies (Maven) |
| 116 | |
| 117 | ```xml |
| 118 | <!-- JAXB (if needed) --> |
| 119 | <dependency> |
| 120 | <groupId>jakarta.xml.bind</groupId> |
| 121 | <artifactId>jakarta.xml.bind-api</artifactId> |
| 122 | <version>4.0.1</version> |
| 123 | </dependency> |
| 124 | <dependency> |
| 125 | <groupId>org.glassfish.jaxb</groupId> |
| 126 | <artifactId>jaxb-runtime</artifactId> |
| 127 | <version>4.0.4</version> |
| 128 | <scope>runtime</scope> |
| 129 | </dependency> |
| 130 | |
| 131 | <!-- Annotation API --> |
| 132 | <dependency> |
| 133 | <groupId>jakarta.annotation</groupId> |
| 134 | <artifactId>jakarta.annotation-api</artifactId> |
| 135 | <version>2.1.1</version> |
| 136 | </dependency> |
| 137 | ``` |
| 138 | |
| 139 | ### Module System Issues |
| 140 | |
| 141 | If using reflection on JDK internals, add JVM flags: |
| 142 | ```bash |
| 143 | --add-opens java.base/java.lang=ALL-UNNAMED |
| 144 | --add-opens java.base/java.util=ALL-UNNAMED |
| 145 | ``` |
| 146 | |
| 147 | **Maven Surefire:** |
| 148 | ```xml |
| 149 | <plugin> |
| 150 | <groupId>org.apache.maven.plugins</groupId> |
| 151 | <artifactId>maven-surefire-plugin</artifactId> |
| 152 | <configuration> |
| 153 | <argLine> |
| 154 | --add-opens java.base/java.lang=ALL-UNNAMED |
| 155 | </argLine> |
| 156 | </configuration> |
| 157 | </plugin> |
| 158 | ``` |
| 159 | |
| 160 | ### New Features to Adopt |
| 161 | |
| 162 | ```java |
| 163 | // var (local variable type inference) |
| 164 | var list = new ArrayList<String>(); // instead of ArrayList<String> list = ... |
| 165 | |
| 166 | // String methods |
| 167 | " hello ".isBlank(); // true for whitespace-only |
| 168 | " hello ".strip(); // better trim() (Unicode-aware) |
| 169 | "line1\nline2".lines(); // Stream<String> |
| 170 | "ha".repeat(3); // "hahaha" |
| 171 | |
| 172 | // Collection factory methods (Java 9+) |
| 173 | List.of("a", "b", "c"); // immutable list |
| 174 | Set.of(1, 2, 3); // immutable set |
| 175 | Map.of("k1", "v1"); // immutable map |
| 176 | |
| 177 | // Optional improvements |
| 178 | optional.ifPresentOrElse( |
| 179 | value -> process(value), |
| 180 | () -> handleEmpty() |
| 181 | ); |
| 182 | |
| 183 | // HTTP Client (replaces HttpURLConnection) |
| 184 | HttpClient client = HttpClient.newHttpClient(); |
| 185 | HttpRequest request = HttpRequest.newBuilder() |
| 186 | .uri(URI.create("https://api.example.com")) |
| 187 | .build(); |
| 188 | HttpResponse<String> response = client.send(request, |
| 189 | HttpResponse.BodyHandlers.ofString()); |
| 190 | ``` |
| 191 | |
| 192 | - |