{"record":{"id":"6cf4338cb0ebadf5","repo":"testcontainers/testcontainers-java","slug":"can-not-access-container-defined-in-field","errorCode":null,"errorMessage":"Can not access container defined in field {}","messagePattern":"Can not access container defined in field (.+?)","errorType":"exception","errorClass":"ExtensionConfigurationException","httpStatus":null,"severity":"error","filePath":"modules/junit-jupiter/src/main/java/org/testcontainers/junit/jupiter/TestcontainersExtension.java","lineNumber":254,"sourceCode":"                        String.format(\"FieldName: %s does not implement Startable\", field.getName())\n                    );\n                }\n                return true;\n            }\n            return false;\n        };\n    }\n\n    private static StoreAdapter getContainerInstance(final Object testInstance, final Field field) {\n        try {\n            field.setAccessible(true);\n            Startable containerInstance = (Startable) field.get(testInstance);\n            if (containerInstance == null) {\n                throw new ExtensionConfigurationException(\"Container \" + field.getName() + \" needs to be initialized\");\n            }\n            return new StoreAdapter(field.getDeclaringClass(), field.getName(), containerInstance);\n        } catch (IllegalAccessException e) {\n            throw new ExtensionConfigurationException(\"Can not access container defined in field \" + field.getName());\n        }\n    }\n\n    /**\n     * An adapter for {@link Startable} that implement {@link CloseableResource}\n     * thereby letting the JUnit automatically stop containers once the current\n     * {@link ExtensionContext} is closed.\n     */\n    private static class StoreAdapter implements CloseableResource, AutoCloseable {\n\n        @Getter\n        private String key;\n\n        private Startable container;\n\n        private StoreAdapter(Class<?> declaringClass, String fieldName, Startable container) {\n            this.key = declaringClass.getName() + \".\" + fieldName;\n            this.container = container;","sourceCodeStart":236,"sourceCodeEnd":272,"githubUrl":"https://github.com/testcontainers/testcontainers-java/blob/8e549514e3f01c57d70546fbb8599d138f3903e5/modules/junit-jupiter/src/main/java/org/testcontainers/junit/jupiter/TestcontainersExtension.java#L236-L272","documentation":"Thrown when the extension cannot read the @Container field via reflection because field.setAccessible(true) or field.get(testInstance) raised IllegalAccessException. This happens when the JVM security model or module access rules prevent accessing the field, typically non-public fields in restricted packages or strong encapsulation (JPMS) blocking reflection.","triggerScenarios":"getContainerInstance calls field.setAccessible(true)/field.get(testInstance) on a field whose declaring class/package is not open to the testcontainers extension, and IllegalAccessException is rethrown as ExtensionConfigurationException.","commonSituations":"Container fields declared private in a named module that does not open the package; running on JDK 16+ with strong encapsulation and no --add-opens; security manager or native-image restricting reflection; fields on a superclass in a different non-opened package.","solutions":["Make the container field public (or at least accessible) and public static for @Container static fields","Add JVM args like --add-opens <module>/<package>=ALL-UNNAMED for the declaring package","If using JPMS, add 'opens <package>;' to the module-info of the test classes","Check that the field's declaring class is actually loaded from the test classpath, not a duplicate class in another classloader"],"exampleFix":"// before\n@Container\nprivate GenericContainer<?> redis = new GenericContainer<>(\"redis:7\");\n\n// after\n@Container\npublic GenericContainer<?> redis = new GenericContainer<>(\"redis:7\");\n// or run tests with: --add-opens com.example.tests/com.example.tests=ALL-UNNAMED","handlingStrategy":"validation","validationCode":"// check the field is reflective-accessible before the test runs\nField f = testClass.getDeclaredField(\"redis\");\ntry { f.setAccessible(true); }\ncatch (InaccessibleObjectException e) { throw new IllegalStateException(\"Add --add-opens for \" + f.getDeclaringClass().getPackageName(), e); }","typeGuard":null,"tryCatchPattern":"try {\n  extension.processContainers(...);\n} catch (ExtensionConfigurationException e) {\n  if (e.getMessage().startsWith(\"Can not access container\")) {\n    // add --add-opens or make the field public, then re-run\n  }\n  throw e;\n}","preventionTips":["Declare @Container fields public","Avoid named modules for test classes; if used, add 'opens <package>;'","On JDK 16+ add --add-opens JVM args when reflection is needed","Keep container fields in the test class itself, not a superclass in another package"],"tags":["junit","reflection","java-modules"],"backgroundTag":"permission-denied","analyzedSha":"8e549514e3f01c57d70546fbb8599d138f3903e5","analyzedAt":"2026-09-12T14:56:41.227Z","contentChangedAt":"2026-09-12T14:56:41.227Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}