4

I am using Eclipse (javafx) and Scene Builder developing an app and I need to use a webview. My program is working smoothly, but when I add a webview component into an anchor pane, I got this error.

Caused by: java.lang.ClassNotFoundException: javafx.scene.web.WebView
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javafx.fxml/javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2931)
at javafx.fxml/javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2920)
at javafx.fxml/javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2861)
... 16 more
Exception running application view.Main

I tried to use the initialize function but still getting same errors, Can anyone tell me why? Thanks in advance.

Ali Hasan
  • 53
  • 1
  • 4
  • 3
    Is the `javafx.web` module on the class-path/module-path? – Slaw Mar 22 '20 at 23:31
  • @Slaw I am using two classes the first one is the main class and the second one the controller class. The web view component in the controller class. Th problem is only when i add the webview i get this error – Ali Hasan Mar 22 '20 at 23:34
  • Yes, but `WebView` is not part of your project—it's from a third-party dependency. If you don't have the `javafx.web` module on the module-path then you'll get your `ClassNotFoundException`. – Slaw Mar 22 '20 at 23:36
  • @Slaw Ok then, How can I use the javafx,web? – Ali Hasan Mar 22 '20 at 23:39
  • By placing the `javafx.web` module on the module-path. You appear to already have the knowledge regarding how to accomplish that; the provided stack trace shows the `javafx.fxml` module is on the module-path, just do what you did for `javafx.fxml` but for `javafx.web` as well. – Slaw Mar 22 '20 at 23:41
  • @Slaw I tried and it is same – Ali Hasan Mar 23 '20 at 12:19
  • 1
    Then please provide a [mre] demonstrating the problem. – Slaw Mar 23 '20 at 18:22
  • 1
    I've had the same issue and by adding "requires javafx.web;" in my module-info.java file it successfully solved the issue. – user3554898 Oct 30 '20 at 22:53
  • Does this answer your question? [Module error when running JavaFx media application](https://stackoverflow.com/questions/53237287/module-error-when-running-javafx-media-application) – kleopatra Feb 22 '21 at 10:36

2 Answers2

9

After i had this exact same error for a longtime, i solved adding the following arguments on eclipse>run>run configurations... then select the "Arguments" tab and add the text bellow to the "VM Arguments"

--module-path "%PHYSICAL_PATH_TO_JAVAFX%\lib" --add-modules javafx.controls,javafx.fxml,javafx.web

The main problem was that everywhere you go, they tell you to add the controls and fxml modules, but they never say that you may need to add more modules to those arguments. so i added the "javafx.web" and it solved my problem.

GRCarvalho
  • 106
  • 1
  • 5
  • _main problem was that everywhere you go, they tell you to add the controls and fxml modules, but they never say_ the main problem is that most developers think it optional to learn the basics of java ;) You __must__ understand the notion of modules to a certain depths, there's no short-cut. And it's not exactly rocket science to expect that accessing a class in a package in a module requires that module to be known to the context ... – kleopatra Feb 22 '21 at 10:35
  • The problem is somewhere else. Since Java 11, JavaFX module has been removed from JDK, so it now has to be explicitly included, and this is where problems origin. No one compiles Java projects manually using `javac`; people would typically use Gradle to include JavaFX, but this requires setting up additional build options to include appropriate modules, people were already struggling with this: https://discuss.gradle.org/t/gradle-doesnt-add-modules-to-module-path-during-compile/27382. Thankfully, there's a Gradle plugin addressing this issue: https://openjfx.io/openjfx-docs/#gradle. – itachi Jul 16 '21 at 11:57
0

In Gradle, make sure to add javafx.web in the modules list under JavaFX. Example below.

javafx {
    version = '17.0.6'
    modules = ['javafx.controls', 'javafx.fxml', 'javafx.web']
}

Then, for module-info.java, make sure to add requires javafx.web;.

Macintosh Fan
  • 355
  • 1
  • 4
  • 18