2

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

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • 2
    The issue seems already filed and fixed in the Playwright Java repository and got released as an alpha version, see here: https://github.com/microsoft/playwright-java/issues/423 – Max Schmitt May 06 '21 at 08:55

1 Answers1

3

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    but with Java 17: *"VM warning: Ignoring option --illegal-access=permit; support was removed in 17.0"* - [Release Notes](https://www.oracle.com/java/technologies/javase/17-relnote-issues.html#JDK-8266851) – user16320675 Oct 18 '21 at 18:48
  • 1
    Following seems to have the same effect and works on Java 17: `--add-opens java.base/java.util=ALL-UNNAMED` and in case you want to pass it to a maven build: `mvn test -DargLine="--add-opens java.base/java.util=ALL-UNNAMED"` – SimonT Oct 19 '21 at 13:29