Java 17升级后InaccessibleObjectException问题求助(Jenkins环境)
Java 17升级后Jenkins测试抛出InaccessibleObjectException,--add-opens配置无效
我们最近从Java 8升级到Java 17,遇到了Java模块化相关的问题。本地环境里,我通过添加以下JVM参数解决了部分单元测试失败的问题:
--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED
在另一个项目中,我在pom.xml里配置maven-surefire-plugin也成功解决了同类问题:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <!-- 支持JUnit5和Java17的版本 --> <configuration> <argLine>--add-opens=java.base/java.time=ALL-UNNAMED</argLine> <argLine>--add-opens=java.base/java.lang=ALL-UNNAMED</argLine> </configuration> </plugin>
但在当前项目中,同样的配置完全不起作用,测试还是报错。
错误堆栈信息
16:09:56 [ERROR] Tests run: 2, Failures: 1, Errors: 1, Skipped: 0, Time elapsed: 0.83 s <<< FAILURE! - in ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest 16:09:56 [ERROR] ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest.testUniqueCompositeKeyConstraintWhenSaving Time elapsed: 0.11 s <<< FAILURE! 16:09:56 org.opentest4j.AssertionFailedError: Unexpected exception type thrown, expected: <javax.persistence.RollbackException> but was: <java.lang.reflect.InaccessibleObjectException> 16:09:56 at ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest.testUniqueCompositeKeyConstraintWhenSaving(OwnBidParameterIntegrationTest.java:92) 16:09:56 Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.time.LocalDate java.time.LocalDateTime.date accessible: module java.base does not "opens java.time" to unnamed module @192d3247 16:09:56 at ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest.createOwnBidParamEntity(OwnBidParameterIntegrationTest.java:133) 16:09:56 at ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest.lambda$testUniqueCompositeKeyConstraintWhenSaving$2(OwnBidParameterIntegrationTest.java:93) 16:09:56 at ch.irix.asco.export.dimensional.ownbids.entity.OwnBidParameterIntegrationTest.testUniqueCompositeKeyConstraintWhenSaving(OwnBidParameterIntegrationTest.java:92)
已尝试的排查步骤
一开始以为是JSON里的
timestampUtc字段导致的问题,移除后测试依然失败:"timestampUtc": { "date": { "year": 2020, "month": 1, "day": 1 }, "time": { "hour": 10, "minute": 0, "second": 0, "nano": 0 } },实体创建方法:
private OwnBidParameterEntity createOwnBidParamEntity() throws IOException { return JSONReader.readJsonDocument(getClass().getClassLoader(), OwnBidParameterEntity.class, SAMPLE_OWN_BID_PARAM_ENTITY_PATH); }对应的JSON样本(已移除timestampUtc):
{ "volumePe": 600.0, "auctionAward": 90.0, "priceP0": 20.0, "pricePE": 250, "strategy": "NOT YET IMPLEMENTED", "refPriceCH": 0.0, "optimalPrice": 0.0, "priceOffsetPercent": 10, "priceOffset": 20, "calculatedStartPrice": 20.0, "fixedVolume": 3, "maxVolume": 600.0, "volumeFactor": 4, "maxStandingVolume": 500, "minPercentRotating": 0, "interpolationParameters": [ { "stepSize": 5, "volumePercent": 10, "pricePercent": 10, "calculatedPrice": 43.0, "calculatedVolumeManual": 60.0 }, { "stepSize": 5, "volumePercent": 20, "pricePercent": 20, "calculatedPrice": 66.0, "calculatedVolumeManual": 120.0 }, { "stepSize": 5, "volumePercent": 30, "pricePercent": 30, "calculatedPrice": 89.0, "calculatedVolumeManual": 180.0 }, { "stepSize": 5, "volumePercent": 40, "pricePercent": 40, "calculatedPrice": 112.0, "calculatedVolumeManual": 240.0 }, { "stepSize": 5, "volumePercent": 80, "pricePercent": 100, "calculatedPrice": 250.0, "calculatedVolumeManual": 480.0 } ], "startTimeEntity": { "id": 10, "year": 2020, "month": 1, "day": 1, "hour": 10, "minute": 0, "cetOffsetToUtc": 1 }, "endTimeEntity": { "id": 11, "year": 2020, "month": 1, "day": 1, "hour": 11, "minute": 0, "cetOffsetToUtc": 1 }, "marketParticipantEntity": { "id": 1, "marketParticipantMRid": "XYZ" }, "productEntity": { "businessType": "A10", "productName": "TRE_PLUS", "direction": "A01", "activationConstraint": "PT15M" }, "marketEntity": { "id": 1, "area": "CH", "auctionMRId": "something" }, "ascoBidId": 42 }
当前使用的Java版本是17.0.8,有没有人能给点解决思路?万分感谢!
内容的提问来源于stack exchange,提问作者xcrookedxedge
相关产品推荐
相关产品推荐

