Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't SBT find JavaFX packages in Java

Tags:

java

javafx

sbt

I wanted to try and make a simple JavaFX app in sbt, but it seems sbt is unable to locate any of the javafx packages, giving me errors like

error: package javafx.application does not exist
error: package javafx.fxml does not exist
error: package javafx.scene does not exist

... and so on

And I find that odd given the fact that the javafx package is included in Java 7+ by default, so if anything, the packages SHOULD be available to the compiler, but it doesn't seem that way..

Any help?

ps: I'm not using any javafx related plugins, just pure sbt, and I'm trying to compile a Java project, not a Scala one. The project is set up to be compatible with Eclipse using sbteclipse

like image 978
Electric Coffee Avatar asked Mar 18 '23 16:03

Electric Coffee


1 Answers

How to build something against JavaFX (in SBT or any other tool) depends a lot on your version of the JDK:

Using JDK 8

It all works out of the box: JavaFX is located in jre/lib/ext, which means it is on the default classpath of java and javac, and it should be available automatically both when compiling and running. That's the configuration @JacekLaskowski has in his answer.

This only works if you only target Java 8: JavaFX 8 is not available for Java 7, so compiling against it makes your app Java8-only (unless you make sure to only use things available in JavaFX 2.x, target jdk7 bytecode, package JavaFX 2.x with your app, etc.)

Using JDK 7u6+

The JavaFX SDK is distributed with the JDK, but it is not available automatically: it is not on the classpath of anything, you have to look for it in jre/lib and add it to the classpath by yourself. That's what some IDEs do automatically when they have JavaFX support.

SBT doesn't do that automatically for you. There is sbt-javafx that helps a little, but you still need to configure the location of jars, etc.

Using JDK 6 to JDK 7u5

You have to download the standalone version and add it to the classpath. The rest of the jdk7u6+ case above applies.


Finally, note that new features are added to JavaFX during the lifetime of Java 8, so building you app may require a specific version of JDK8 (this also happened a little in JDK7), and that's also one of the reasons you are supposed to package JavaFX with your application.

Basically as soon as you depend on JavaFX, you have to track the JDK and/or JavaFX itself as two unmanaged dependencies, with individual developers having to check versions and configure things.

like image 76
gourlaysama Avatar answered Mar 29 '23 08:03

gourlaysama