This document provides detailed analysis and migration steps for upgrading a project from Java 8 to Java 17, including code modification suggestions, dependency updates, and configuration tweaks.
1. Project configuration update
1.1 Maven configuration
Problem location: and submodules
<properties> <>1.8</> <>1.8</> <>1.8</> </properties>
Revision suggestions: Updated Maven configuration to support Java 17:
<properties> <>17</> <>17</> <>17</> </properties>
1.2 Maven plugin update
Problem location:
<plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin>
Revision suggestions: Update the Maven compile plugin configuration:
<plugin> <groupId></groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.10.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> <compilerArgs> <arg>--add-opens=/=ALL-UNNAMED</arg> <arg>--add-opens=/=ALL-UNNAMED</arg> <arg>--add-opens=/=ALL-UNNAMED</arg> <arg>--add-opens=/=ALL-UNNAMED</arg> </compilerArgs> </configuration> </plugin>
1.3 Spring Boot version update
Problem location:
<parent> <groupId></groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> <relativePath/> </parent>
Revision suggestions: Update Spring Boot version at least 2. or higher (recommended) to ensure full compatibility with Java 17:
<parent> <groupId></groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.5</version> <relativePath/> </parent>
Notice: If you upgrade to Spring Boot, you need to handle Jakarta EE-related package name changes (javax.* becomes jakarta.*).
2. Removed Java EE modules
Java 9 and above removes many Java EE modules that need to be added as separate dependencies.
2.1 JAXB (Java Architecture for XML Binding)
question: Java 9+ RemovedBag
Revision suggestions: Add the following dependencies to:
<dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>4.0.0</version> </dependency> <dependency> <groupId></groupId> <artifactId>jaxb-runtime</artifactId> <version>4.0.2</version> </dependency>
2.2 JavaBeans Activation Framework (JAF)
question: Java 9+ RemovedBag
Revision suggestions: Add the following dependencies to:
<dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId></groupId> <artifactId>angus-activation</artifactId> <version>2.0.0</version> </dependency>
2.3 Java Annotation API
Problem location: used in multiple files.*
Bag
import ;
Revision suggestions: Add the following dependencies to:
<dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>2.1.1</version> </dependency>
And update the import statement:
import ;
2.4 Java Mail API
Problem location: /core/alarm/impl/
Revision suggestions: Add the following dependencies:
<dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId></groupId> <artifactId></artifactId> <version>2.0.1</version> </dependency>
2.5 Java Servlet API
Problem location: used in multiple files.*
Bag
import ;
Revision suggestions: Add the following dependencies to:
<dependency> <groupId></groupId> <artifactId>-api</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency>
And update the import statement:
import ;
3. Reflection API changes
3.1 Reflection access to non-public members
Problem location:
xxx/common/util/
xxx/config/mybatis/
xxx/calc/utils/
Problem description: Java 9+ has made stricter encapsulation of the reflection API, especially with stricter access restrictions on non-public members.
Revision suggestions:
1. Ensure accessibility is explicitly set when accessing non-public members and restored after use:
try { (true); Object value = (object); // Use value return value; } finally { (false); }
2. If encountered at runtimeInaccessibleObjectException
, you need to add JVM parameters:
--add-opens /=ALL-UNNAMED --add-opens /=ALL-UNNAMED
3.2 Reflection acquisition methods and fields
Problem location:
xxx/service/impl/
Revision suggestions:
- use
()
replace()
To obtain non-public methods - Ensure that generic types are handled correctly
- Add appropriate exception handling
try { Method method = (methodName, parameterTypes); (true); return (object, args); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException("Reflection call method failed", e); } finally { // Optional: Restore access control}
4. Internal API usage
4.1 sun.* package usage
Problem description: Java 9+ is strictly restrictedsun.*
Access to package.
Revision suggestions:
- Check if it is used in the project
sun.*
The packaged class - If so, look for standard API alternatives
- If you have to use it, consider using it
--add-exports
or--add-opens
Command line parameters
5. Language feature optimization
5.1 Switch expressions
Problem location:
xxx/common/util/text/
xxx/common/system/query/
xxx/biz/service/impl/
Revision suggestions: Use Java 12+'s Switch expression syntax, which is more concise and safer:
// Old codedouble result; switch (operator) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; // ... } return result; // New codedouble result = switch (operator) { case "+" -> num1 + num2; case "-" -> num1 - num2; case "*" -> num1 * num2; case "/" -> { if (num2 == 0) { ("The divisor cannot be zero"); yield 0.0; } yield num1 / num2; } default -> { ("Unsupported operators: {}", operator); yield 0.0; } }; return result;
5.2 Text blocks
Problem location: There are many codes in the project that use long string splicing
Revision suggestions: Use the text block features of Java 15+ to improve code readability:
// Old codeString sql = "SELECT * " + "FROM users " + "WHERE status = 'active' " + "AND created_date > ?"; // New codeString sql = """ SELECT * FROM users WHERE status = 'active' AND created_date > ? """;
5.3 instanceof pattern matching
Problem location:
xxx/common/aspect/
Revision suggestions: Simplify type checking and conversion using instanceof pattern matching in Java 16+:
// Old codeif (result instanceof Result) { if (((Result) result).getData() instanceof IPage) { // Use ((Result) result).getData() } } // New codeif (result instanceof Result res && () instanceof IPage page) { // Use page directly}
5.4 Record Type
Revision suggestions: For simple data transfer objects, consider using Java 16+ Record type:
// Old codepublic class UserDTO { private final String name; private final String email; public UserDTO(String name, String email) { = name; = email; } // getters, equals, hashCode, toString } // New codepublic record UserDTO(String name, String email) {}
6. Third-party library compatibility
6.1 Spring Boot and Spring Framework
Problem location:
<>2.7.18</> <>2021.0.0</> <>2021.1</>
Revision suggestions: Updated to a version that supports Java 17:
<>3.1.5</> <>2022.0.4</> <>2022.0.0.0</>
6.2 MyBatis and MyBatis-Plus
Problem location:
<>3.5.1</> <>4.1.3</>
Revision suggestions: Updated to a version that supports Java 17:
<>3.5.4.1</> <>4.2.0</>
6.3 Shiro
Problem location:
<>1.12.0</> <>3.11.0</> <>3.2.2</>
Revision suggestions: Shiro 1.12.0 supports Java 17, but it is recommended to update the JWT library:
<>4.4.0</>
6.4 Database Driver
Problem location:
<>42.2.25</> <>11.2.0.3</> <>4.0</> <>8.0.27</>
Revision suggestions: Updated to a version that supports Java 17:
<>42.6.0</> <>23.3.0.23.09</> <!-- replace ojdbc6 --> <>12.4.2.jre11</> <!-- replace sqljdbc4 --> <>8.2.0</> <!-- replace mysql-connector-java -->
And update the dependency statement:
<!-- MySQL --> <dependency> <groupId></groupId> <artifactId>mysql-connector-j</artifactId> <version>${}</version> </dependency> <!-- Oracle --> <dependency> <groupId></groupId> <artifactId>ojdbc11</artifactId> <version>${}</version> </dependency> <!-- SQL Server --> <dependency> <groupId></groupId> <artifactId>mssql-jdbc</artifactId> <version>${}</version> </dependency>
6.5 Log Framework
Problem location:
<>2.17.0</> <>1.2.9</>
Revision suggestions: Updated to a version that supports Java 17:
<>2.22.0</> <>1.4.11</>
6.6 JSON Processing Library
Problem location:
<>1.2.83</>
Revision suggestions: Update to a version that supports Java 17, or consider switching to Jackson:
<>2.0.42</>
And update the dependencies:
<groupId>.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>${}</version> </dependency>
6.7 Other tool library
Problem location:
<>5.8.25</> <>29.0-jre</> <>2.6</> <>2.11.0</> <>1.9.4</>
Revision suggestions: Updated to a version that supports Java 17:
<>32.1.3-jre</> <>3.13.0</> <!-- replace --> <>2.15.0</> <>1.9.4</> <!-- It's the latest version -->
7. Module system compatibility
Compatibility for non-modular applications
Problem description: Java 9+ introduces module systems that may affect the behavior of non-modular applications.
Revision suggestions:
- Add to
--add-modules
Parameters to access the removed module - Add to
--add-opens
Parameters to allow reflection access - Consider migrating your application to a module system
8. Safety-related changes
8.1 Encryption algorithm support
Problem location:
xxx/common/util/security/
xxx/system/service/util/
Problem description: Java 17 may remove some unsafe encryption algorithms.
Revision suggestions:
- Check if the encryption algorithm used is still supported in Java 17
- Update to safer algorithms and longer key lengths
- Using the new security features of Java 17
8.2 TLS configuration
Problem location:
xxx/common/util/
Problem description: Java 17 has disabled some unsafe TLS versions and password suites by default.
Revision suggestions:
- Update SSL/TLS configuration to use TLS 1.3 or TLS 1.2
- Remove support for unsafe cipher suites
- Use a safer certificate verification mechanism
9. String and text processing
9.1 String processing method
Problem description: Java 11+ introduces many new string processing methods.
Revision suggestions: Use Java 11+ to improve code efficiency:
- use
()
replace()
- use
()
Check for blank strings - use
()
Processing multiple lines of text - use
(n)
Repeat string
9.2 Regular expression improvements
Problem description: Java 11+ improved the regular expression API.
Revision suggestions: Using Java 11+ improved regular expression API:
- use
()
Create a predicate - Simplify regular expressions using named capture groups
10. Network API changes
HTTP Client
Problem location:
xxx/calc/utils/
Problem description: Java 11 introduces a new HTTP client API, and the old HTTP client API may be removed in future versions.
Revision suggestions: Consider using the new HTTP client API for Java 11+:
HttpClient client = () .version(.HTTP_2) .followRedirects() .connectTimeout((20)) .sslContext(sslContext) .build(); HttpRequest request = () .uri((url)) .header("Content-Type", "application/json") .POST((jsonBody)) .build(); HttpResponse<String> response = (request, ());
11. Collection and Stream API Enhancement
11.1 Collection factory method
Revision suggestions: Create immutable collections using Java 9+'s collection factory method:
// Old codeList<String> list = (("a", "b", "c")); Map<String, Integer> map = (new HashMap<String, Integer>() {{ put("one", 1); put("two", 2); }}); // New codeList<String> list = ("a", "b", "c"); Map<String, Integer> map = ("one", 1, "two", 2);
11.2 Stream API Enhancement
Revision suggestions: Leverage Java 9+ enhanced Stream API:
- use
()
Process elements that may be null - use
()
New overloading method - use
takeWhile()
anddropWhile()
method - use
()
replacecollect(())
12. Testing suggestions
After migrating to Java 17, the following tests are recommended:
- Compilation test: Make sure that all code can be compiled and passed under Java 17
- Unit Test: Run all unit tests to ensure normal functionality
- Integration testing: Testing integration with external systems
- Performance Test: Java 17 has significantly improved performance, but may require tuning in some cases
- Security testing: Ensure encryption and security-related functions work properly
13. Migration step suggestions
1. Preparation phase
- Create a project branch for migration
- Update JDK version to Java 17
- Update Maven configuration
2. Dependency update phase
- Update Spring Boot and Spring Framework
- Update the database driver
- Update other third-party libraries
3. Code modification stage
- Add missing Java EE module dependencies
- Fix Reflection API usage
- Handle internal API access issues
- Update package import (javax.* → jakarta.* if using Spring Boot 3)
4. Optimization stage
- Optimize code with new features in Java 9-17
- Update security-related configurations
- Update network API usage
5. Testing phase
- Run unit tests
- Carry out integration testing
- Perform performance testing
6. Deployment phase
- Update deployment scripts and configurations
- Deployment in the test environment
- Monitor application performance and stability
in conclusion
Upgrading a project from Java 8 to Java 17 is an important effort that can bring performance improvements, security enhancements, and new feature support. The main migration efforts include:
- Update Maven configuration and dependencies
- Add removed Java EE module dependencies
- Fix Reflection API usage
- Handle internal API access issues
- Update third-party library version
- Optimize code with new features in Java 9-17
By systematically addressing these issues, projects can be moved smoothly to Java 17 and take advantage of the benefits of the new version.
The above is the detailed content of the compatibility analysis and migration guide for upgrading java8 to java17. For more information about upgrading java8 to java17, please follow my other related articles!