0

I'm trying to get some java 1.8 code working in Java 11. I've hit what seems to be a common wall, but none of the answers I've found are working.

System: Fedora 33 5.9.8-200.fc33.x86_64
Eclipse Version: 2020-09 (4.17.0) Build id: 20200910-1200
JDK: java-11-openjdk-11.0.9.11-0.fc33.x86_64
JavaFX: OpenFX javafx-sdk-11.0.2

Here's the bit of code that's blocking me.

class MyPie extends PieChart {
   public Legend legend;
   
   public MyPie() {
      super();
      legend = (Legend) getLegend();
   }
}

The error is: "Legend cannot be resolved to a type" When I ask Eclipse to auto-generate an import statement, or use the suggestion, it put in this line:

import com.sun.javafx.charts.Legend;

This is a problem, because that doesn't seem to exist and doesn't follow the same pattern as the other JavaFX imports:

import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;

I have a second one "com.sun.javafx.css.StyleManager" which I'm guessing will have the same solution.

What am I missing here?

Thanks.

  • 2
    `com.sun` packages and classes are not part of the public API and are not guaranteed to be available in future releases. You use them explicitly assuming the risk this may happen. – James_D Nov 19 '20 at 21:07
  • 2
    If you **must** use private API then you'll need to use `--add-exports` and/or `--add-opens` to grant your module (or all unnamed modules) access. These command line options must be specified at compile-time _and_ run-time. – Slaw Nov 19 '20 at 21:13
  • @James_D I guess that's really my question... Were these deprecated in OpenFX between 8 and 11? Did the name change? I can't find any information about these, or how to replace them with something public. Slaw: Where do I do that in Eclipse? – Phantom of Krankor Nov 19 '20 at 21:28
  • 1
    Neither of those: the class [still exists in the current version](https://github.com/openjdk/jfx/blob/master/modules/javafx.controls/src/main/java/com/sun/javafx/charts/Legend.java). You can't access it because of the new module system in Java 9 and later, which is *designed to prevent you from using non-public API*. You can work around it as described in @Slaw's comment. (I believe you need something like `--add-exports=javafx.controls/com.sun.javafx.charts=ALL-UNNAMED` to both the compiler and JVM) – James_D Nov 19 '20 at 21:30
  • I'm still floundering here. If they've been made private and might be removed in future released, are there not public replacement for them? I think I've sort of found where to put that in, but it's still not right. https://stackoverflow.com/questions/54068992/how-to-tell-eclipse-to-add-exports-when-compiling/54071487 refers to a deprecated dialog box. When I go to the new version, I see that com.sun.javafx.css is in javafx.graphics. That seems to have worked. However, com.sun.javafx.charts isn't showing up as being in javafx.controls (or any of the other javafx libraries). – Phantom of Krankor Nov 19 '20 at 21:52
  • 1
    And I just searched for `Legend` and it's definitely in the `com.sun.javafx.charts` package which is in the `javafx.controls` module (at least in JavaFX 15). "_If they've been made private and might be removed in future released, are there not public replacement for them?_" – they were **always** private, the module system just makes this explicit; there can't be a replacement because there's nothing to replace. – Slaw Nov 19 '20 at 21:59
  • I've managed to add "--add-exports javafx.controls/com.sun.javafx.charts=ALL-UNNAMED --add-exports javafx.graphics/com.sun.javafx.css=ALL-UNNAMED" but now all I get is "WARNING: Unknown module: javafx.graphics specified to --add-exports WARNING: Unknown module: javafx.controls specified to --add-exports" when I run. – Phantom of Krankor Nov 19 '20 at 22:00
  • That sounds like you aren't putting JavaFX on the module-path, but rather the class-path. If you haven't already, check out [Getting Started with JavaFX](https://openjfx.io/openjfx-docs/) to see how to properly setup JavaFX in Eclipse. – Slaw Nov 19 '20 at 22:03
  • I thought that might have been the case as well, but Eclipse is telling me that this is being put on the command line "-p /home/Greg/src/jfx/javafx-sdk-11.0.2/lib/javafx.base.jar:/home/Greg/src/jfx/javafx-sdk-11.0.2/lib/javafx.controls.jar:..." It's also not helpful that that link talks about using Java 15, when Eclipse 2020-09 doesn't actually support it as an environment... – Phantom of Krankor Nov 19 '20 at 22:09
  • OK. I also had to mysteriously add a module import statement for javafx.web, even though it's in the list of modules to be loaded. "--module-path /home/Greg/jfx/javafx-sdk-11.0.2 --add-modules javafx.controls,javafx.graphics --add-exports javafx.controls/com.sun.javafx.charts=ALL-UNNAMED --add-exports javafx.graphics/com.sun.javafx.css=ALL-UNNAMED --add-modules javafx.web" seems to be doing the trick. – Phantom of Krankor Nov 19 '20 at 22:25

0 Answers0