'JavaFX access internal webkit document
I am trying to access javafx.web/com.sun.webkit.dom
from my unnamed module javafx project. For this, I have created a class com.sun.webkit.dom.DomMapper
.
I do not see any error in the IDE, but when I run it using mvn clean javafx:run
it complains -
Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: com/sun/webkit/dom/DomMapper
....
Caused by: java.lang.ClassNotFoundException: com.sun.webkit.dom.DomMapper
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
...
This is how my pom.xml looks
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId> ... </groupId>
<artifactId> ... </artifactId>
<version> ... </version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<exec.mainClass> .... </exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.4</version>
<configuration>
<options>
<option>--add-exports</option><option>javafx.web/com.sun.webkit.dom=ALL-UNAMED</option>
<option>--add-opens</option><option>javafx.web/com.sun.webkit.dom=ALL-UNAMED</option>
</options>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerArgs>
<arg>--add-exports</arg><arg>javafx.web/com.sun.webkit.dom=ALL-UNAMED</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
If I use the Maven shade plugin, I am able to bypass this issue. I think it has got to do with how JPMS security works, but I cannot figure out how to bypass this. I tried add-exports, and add-opens in various combinations, but it did not help me. What am I doing wrong?
BTW, I want to map the WebKit DOM to the JSoup dom element, as they give a nice API for CSS selectors.
Update I am adding an additional stack trace of the error
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NoClassDefFoundError: com/sun/webkit/dom/DomMapper
at xyz.jphil.internal_browser.TestApp.start(TestApp.java:86)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.ClassNotFoundException: com.sun.webkit.dom.DomMapper
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 10 more
Solution 1:[1]
The solution for this problem lies in using using --patch-module
. As I have added a class - com.sun.webkit.dom.DomMapper
which is completely sealed by JPMS. The reason why I added DomMapper
in the com.sun.webkit.dom.DomMapper
was to access some package-private functions.
However, I see that can be easily avoided so I am choosing to avoid using those package-private functions. With this --patch-module
is not required, which is also not a recommended thing to do as per JPMS documentation. Now the solution is simpler. Adding --add exports
is enough (which is already there in the POM) and I am able to get it working.
Finally exporting the entire program as a farjar using maven shade plugin for example, also works. This I am adding just for information.
This answer is based on comments and valuable inputs by Jewelsea and Slaw. Further notes:
- This example shows the public (although internal) functions of the
com.sun.webkit.dom
are quite good as it is, gist.github.com/jewelsea/6722397 - The problem I created by making a class inside
com.sun.webkit.dom
is called split-package - read here How split packages are avoided in Java 9
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ivan Velikanov |