Christian Baumann
Christian Baumann

Reputation: 3444

Playwright Java: example returns error: Unable to make field [..] accessible

I'm trying to run the example from the Playwright Java documentation: https://playwright.dev/java/docs/intro#usage

And I am getting this error:

java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object java.util.Optional.value accessible: module java.base does not "opens java.util" to unnamed module @3945bf41
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:357)
    at java.lang.reflect.AccessibleObject.checkCanSetAccessible (AccessibleObject.java:297)
    at java.lang.reflect.Field.checkCanSetAccessible (Field.java:177)
    at java.lang.reflect.Field.setAccessible (Field.java:171)
    at com.google.gson.internal.reflect.UnsafeReflectionAccessor.makeAccessible (UnsafeReflectionAccessor.java:44)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields (ReflectiveTypeAdapterFactory.java:159)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create (ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter (Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField (ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields (ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create (ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter (Gson.java:458)
    at com.google.gson.Gson.fromJson (Gson.java:931)
    at com.google.gson.Gson.fromJson (Gson.java:897)
    at com.google.gson.Gson.fromJson (Gson.java:846)
    at com.google.gson.Gson.fromJson (Gson.java:817)
    at com.microsoft.playwright.impl.Utils.convertViaJson (Utils.java:37)
    at com.microsoft.playwright.impl.BrowserImpl.newPageImpl (BrowserImpl.java:186)
    at com.microsoft.playwright.impl.BrowserImpl.lambda$newPage$2 (BrowserImpl.java:182)
    at com.microsoft.playwright.impl.LoggingSupport.withLogging (LoggingSupport.java:47)
    at com.microsoft.playwright.impl.BrowserImpl.newPage (BrowserImpl.java:182)
    at com.microsoft.playwright.Browser.newPage (Browser.java:562)
    at org.example.Example.main (Example.java:9)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
    at java.lang.Thread.run (Thread.java:831

This is the Example class:

package org.example;

import com.microsoft.playwright.*;

public class Example {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            Browser browser = playwright.chromium().launch();
            Page page = browser.newPage();
            page.navigate("http://playwright.dev");
            System.out.println(page.title());
        }
    }
}

I'm using playwright version 1.10.0, my Java is version 16.0.1, and I'm on Windows 10.

My complete code can be found here: https://github.com/christianbaumann/playwright-sandbox

Upvotes: 2

Views: 3486

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308249

Since Version 16 Java is more protective of its internals and disallows programs from accessing them by default. You can work around that by adding --illegal-access=permit to your java command line.

This SO question explains the underlying technology/decisions. What cought you by surprise is that Java 16 changed how strict it is in the absence of a command line argument. Previous versions only warned, but allowed the access.

Upvotes: 3

Related Questions