1

I run my JavaFX app with VM options:

--module-path "C:\Software\javafx-sdk-18.0.1\lib" --add-modules javafx.base,javafx.controls,javafx.fxml,javafx.graphics

yet when I try to run the app, I get:

Caused by: java.lang.IllegalAccessError: class MyWindow (in unnamed module @0xb083ee4) cannot access class com.sun.javafx.tk.Toolkit (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.tk to unnamed module @0xb083ee4

(As a small note, I rely on FontMetrics, if this makes difference.)

I am wondering what's going on and how to fix it?

coderodde
  • 1,269
  • 4
  • 17
  • 34
  • Is your application intended to be a [tag:java-module] having a [tag:module-info] file? – trashgod Jul 21 '22 at 12:20
  • No, I managed to run another JavaFX app without FontMetrics and module-info.java. Here, I would prefer to avoid making my app a module. – coderodde Jul 21 '22 at 12:26
  • Can you simply remove `module-info.java`? – trashgod Jul 21 '22 at 12:42
  • @trashgod No `module-info.java` to begin with. – coderodde Jul 21 '22 at 13:05
  • 1
    I was wondering what FontMetrics was. I found [another question which refers to it](https://stackoverflow.com/a/32238954/1155209). It would appear to be com.sun api which, generally, should not be used by app code. There are probably command line override switches you can add to your app to allow it to violate encapsulation to access com.sun classes. I do not know what those are and would not post such info here in any case. – jewelsea Jul 21 '22 at 17:41
  • 2
    This is relatively basic module stuff. Modules were added in Java 9, and JavaFX was modularized at the same time. If you don't know what modules are, see [Understanding Java 9 Modules](https://www.oracle.com/corporate/features/understanding-java-9-modules.html). Basically, you're trying to access private API (i.e., a class in a package that is not exported to your code). JavaFX does not want you to use such classes. But if you are okay with relying on private API (which you should avoid), then you need to add an `--add-exports javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED` VM argument. – Slaw Jul 21 '22 at 18:06
  • 2
    That will fix the specific error in your question. If you come across similar errors, just read the error message and add another `--add-exports` argument as needed. The format of the argument is `--add-exports /=`. The `ALL-UNNAMED` value for `` means to export the package to code in the unnamed module, which is where all code on the class-path ends up (as opposed to the module-path). There's one unnamed module per `ClassLoader`, though that's not really important here. – Slaw Jul 21 '22 at 18:08
  • @Slaw Your 2nd last comment solves it! Thank you so much! – coderodde Jul 22 '22 at 08:51

0 Answers0