Java inaccessible object exception

java.lang.reflect.InaccessibleObjectException In Junit with Java17

If I run the Junit test of an application with Java17 (before the application was built up with Java8), I receive this problems:

java.lang.reflect.InaccessibleObjectException: Unable to make void java.text.DecimalFormatSymbols.setPerMillText(java.lang.String) accessible: module java.base does not "opens java.text" to unnamed module @6a1aab78 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) Caused by: java.lang.NullPointerException: Cannot invoke "de.reply.arlanis.vw.dwa.base.db.model.DwaApplication.getMarkets()" because "this.app" is null at de.reply.arlanis.vw.dwa.base.db.model.DwaApplicationTest.initApp(DwaApplicationTest.java:52) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.RunBefores.invokeMethod(RunBefores.java:33) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:54) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:99) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:105) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:40) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:43) at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) 
 @Before public void initApp() throws Exception < app = dwaApplicationService.getDwaApp(); for (Market m : app.getMarkets())< . 

Источник

JUINT test giving error java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.lang.Object.finalize()

While running the test I am getting the error , I am not able to understand why am I getting this error , this code is working with fine in java 8 , while running it in java 17 it is giving error. googled this error but found nothing useful. Please help me to understand this error. Thanks in advance:)

@RunWith(PowerMockRunner.class) @PrepareForTest() @SpringBootTest(classes = MockServletContext.class) @PowerMockIgnore() public class ProjectUserControllerTest < private ProjectUserController controller; private UUID projectId = UUID.randomUUID(); private UUID groupId = UUID.randomUUID(); private String email = "project.user@email.com"; @Mock private ProjectUserService projectUserService; private ObjectMapper objectMapper = new ObjectMapper(); @Mock protected AuthorizedUser au; @Before public void setUp() throws Exception < controller = new ProjectUserController(); FieldUtils.writeField(controller, "projectUserService", projectUserService, true); FieldUtils.writeField(controller, "objectMapper", objectMapper, true); PowerMockito.mockStatic(PopulatedAuthorizedUser.class); Mockito.when(PopulatedAuthorizedUser.get()).thenReturn(mockAuthorizedUser()); >@Test public void testGetProjectUsers() < Mockito.doReturn(Arrays.asList(mockProjectUser())).when(projectUserService) .findProjectUsersByProjectId(projectId); Mockito.doNothing().when(projectUserService).enrichUserDetails(any(ProjectUserDto.class)); ResponseEntity> response=controller.getProjectUsers(projectId); assertNotNull(response); ProjectUserDto projectUserDto = response.getBody().get(0); assertEquals(groupId, projectUserDto.getGroupId()); assertEquals(email, projectUserDto.getUsername()); assertTrue(projectUserDto.getEmailNotification()); assertEquals(ProjectUserRole.OWNER.toString(), projectUserDto.getRole()); > > 
 java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.lang.Object.finalize() throws java.lang.Throwable accessible: module java.base does not "opens java.lang" to unnamed module @5ba23b66 at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) at java.base/java.lang.reflect.Method.checkCanSetAccessible(Method.java:199) at java.base/java.lang.reflect.Method.setAccessible(Method.java:193) at org.powermock.reflect.internal.WhiteboxImpl.doGetAllMethods(WhiteboxImpl.java:1492) at org.powermock.reflect.internal.WhiteboxImpl.getAllMethods(WhiteboxImpl.java:1467) at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:847) at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:807) at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:790) at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466) at org.powermock.modules.junit4.common.internal.impl.PowerMockJUnit4RunListener.testFinished(PowerMockJUnit4RunListener.java:55) at org.junit.runner.notification.SynchronizedRunListener.testFinished(SynchronizedRunListener.java:87) at org.junit.runner.notification.RunNotifier$9.notifyListener(RunNotifier.java:225) at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72) at org.junit.runner.notification.RunNotifier.fireTestFinished(RunNotifier.java:222) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.testAborted(PowerMockJUnit44RunnerDelegateImpl.java:229) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:206) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) 

Источник

Читайте также:  Javascript call Applet example

How to Solve ‘Error: “java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class”’

When working with Java, you may have encountered many errors. But still, java is an important part of the programming world. Programmers learn this language to rule the programming world with exceptionally coded programs. When you are in the phase of learning, you may get ‘Error: “java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class”’ warning shows up after compiling the program.

Beginners should not need to worry as they have not done anything wrong to get the error warning. As you are here to get the issue fixed, we are going to provide you with an exceptional solution that can help you get rid of the error. First, check out how error occurs

How Error Occurs

The main cause of the error is a Java update from version 11 to 17. Sometimes, the new update comes up with some new features and configuration settings that you are not used to, and that’s the reason you get worried when you see the error warning pop up. When you update the version, you get the error after coding

com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549) … 15 more Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class Note: modules in the startup list are –add-opens java.base/java.lang=ALL-UNNAMED –add-opens java.base/java.util=ALL-UNNAMED –add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED 14:18:48 INFO [org.schemarepo.server.RepositoryServer ] Routing java.util.logging traffic through SLF4J Exception in thread “main” com.google.inject.internal.util.$ComputationException: java.lang.ExceptionInInitializerError at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553) at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419) 

Now that you have seen how you can get the error. It’s time to fix the error warning.

Solutions To fix Error: “java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class”

There are a few solutions that are really effective when it comes to solving this error. Check out the solutions

Using VM Parameter

When you upgrade the Java version from 11 to 17, you need to add a parameter at the time of launch.

java --add-opens java.base/java.lang=ALL-UNNAMED

In Java version 17, the -illegal-access becomes ineffective with all the values set on the verge to equivalent to denying. It is still possible for you to use the option -add-opens command-le or the JAR-file Add-open to make specific packages open.

This can solve the error warning in an efficient way.

Include -illegal-access=warn

To get the error warning fixed, you need to include the -illegal-access=warn in your code.

–illegal-access=warn and –add-opens java.base/java.lang=ALL-UNNAMED

You need to add this to your eclipse.ini.

This is the method that can help you solve the ‘Error: “java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class”’ warning.

Conclusion

We have highlighted the solutions to fix ‘Error: “java.lang.reflect.InaccessibleObjectException: Unable to make protected final java.lang.Class”’. Two solutions are discussed, and it is completely on you to choose either according to the projects and errors. Both solutions are perfect to solve the error exception.

On that note, I wish you luck! Hope you find it helpful!

Your feedback is valuable for us so feel free to share your issues and even drop a message if it solves your issue!

Solve the Error “accessible: module java.base does not “opens java.io” to unnamed module in Java”

Java is an object-oriented programming language for creating various applications like web applications, mobile applications, […]

Fix the Error “Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8”

Java is an ideal programming language used for creating infinite applications. Android studio is also […]

Fix the Error “gpg can’t check signature no public key error”

GNU Privacy Guard usually known as GnuPG or GPG is a free and open-source encryption […]

Источник

Оцените статью