2

I am currently working on an application using javafx for its UI. I am using java 9. When I pulled from github to continue development on my new computer and compiled, there were compilation errors at all references of javafx classes, and only javafx classes.

I searched in the jdk in external resources, and the javafx package was indeed where it was supposed to be. I also made a new project using the same jdk and opened a javafx window, which worked fine. This tells me that this is a project-specific issue, rather than a system issue.

Does anyone have some insight into this? Thanks.

EDIT I am using Intellij comunity Ed. The error list was expansive, but here are a few enter image description here

James_D
  • 201,275
  • 16
  • 291
  • 322
Java Noob
  • 351
  • 1
  • 6
  • 15
  • 1
    Did you (or your IDE) define a `module-info.java` file in your Java 9 project? What does the error say, exactly? – James_D Feb 18 '18 at 15:46
  • I updated the question with the error messages – Java Noob Feb 18 '18 at 15:49
  • So is there a `module-info.java` in the project? – James_D Feb 18 '18 at 15:53
  • Yes. quite a few actually – Java Noob Feb 18 '18 at 15:57
  • You will need `requires javafx.controls ;` in the `module-info.java` for the module using JavaFX. (You may need some other modules in addition.) – James_D Feb 18 '18 at 15:59
  • Which other ones? – Java Noob Feb 18 '18 at 16:05
  • It depends what you're using. The [Javadocs](https://docs.oracle.com/javase/9/docs/api/overview-summary.html) organize packages by module, so you can see which packages are in which module (it's fairly intuitive anyway). If you require a module, you get its dependencies too (so requiring `javafx.controls` implicitly gets `javafx.graphics` and `javafx.base`, for example). – James_D Feb 18 '18 at 16:09
  • This worked with no compiler errors! Thanks. If you want, you can post a response and ill mark it correct – Java Noob Feb 18 '18 at 16:12

1 Answers1

4

Your IDE may have set the project up as a "modular" Java 9 project. In modular projects, you specify which other modules the project depends on, and which packages in your module(s) should be exposed to other modules. A good general tutorial on modules is at the OpenJDK wiki.

If there's a module-info.java file in the classpath root, you need to explicitly state that your module requires access to the appropriate JavaFX modules. It may be enough just to add

requires javafx.controls ;

to this file: that will give you access to the javafx.controls module, and the modules on which it depends (javafx.graphics and javafx.base). Depending on your project, you may also need

requires javafx.fxml ;

and/or

requires javafx.web ;

etc.

James_D
  • 201,275
  • 16
  • 291
  • 322