Why SBT can't find JavaFX packages in Java

I wanted to try and make a simple JavaFX application in sbt but it looks like sbt can't find any of the packages javafx

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

      

... etc.

And I find it strange given the fact that the package is javafx

included in Java 7+ by default, so if anything, the packages MUST be available to the compiler, but it doesn't look like ..

Any help?

ps: I am not using any javafx

plugins related , just pure sbt and I am trying to compile a Java project, not Scala. The project is set up for Eclipse compatibility usingsbteclipse

+3


source to share


2 answers


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

Using JDK 8

Everything works out of the box: JavaFX is in jre/lib/ext

, which means it is in the default class path java

and javac

, and it should be automatically available both at compile time and at startup. That's what @ JacekLaskowski's config has in his answer.

This only works if you are targeting only Java 8: JavaFX 8 is not available for Java 7, so compiling against it makes your application Java8 only (unless you are only using what is available in JavaFX 2.x, target jdk7 bytecode , JavaFX 2.x package with your application, etc.)

Using JDK 7u6 +

The JavaFX SDK is distributed with the JDK, but it is not automatically available: it is not on the classpath, you have to look for it in jre/lib

and add it to the classpath yourself. This is what some IDEs do automatically when they have JavaFX support.



SBT does not automatically do this for you. There is sbt-javafx which helps a little, but you still need to tweak the placement of the banners, etc.

Using JDK 6 for JDK 7u5

You need to download the standalone version and add it to your classpath. The rest of the jdk7u6 + case is above.


Finally, please note that new features are added to JavaFX over the lifetime of Java 8, so a specific version of JDK8 may be required to build an application (this also happened a bit in JDK7) and this is also one of the reasons you are supposed to be the JavaFX package must be with your application.

Basically, once you depend on JavaFX, you need to track JDK and / or JavaFX as two unmanaged dependencies, and individual developers need to version check and tweak things.

+7


source


It's weird as I'm new to JavaFX and have never worked with it before, but it works like a charm for me - at least I could import the package javafx.application

.

scala> import javafx.application
import javafx.application

      



You will need to provide additional information about your environment. Mine is downstairs.

> about
[info] This is sbt 0.13.5
[info] The current project is {file:/Users/jacek/sandbox/sbt-learning-space/}sbt-learning-space 1.0.0
[info] The current project is built against Scala 2.10.4
[info] Available Plugins: sbt.plugins.IvyPlugin, sbt.plugins.JvmPlugin, sbt.plugins.CorePlugin, sbt.plugins.JUnitXmlReportPlugin, com.typesafe.sbteclipse.plugin.EclipsePlugin, net.virtualvoid.sbt.graph.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.4

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import javafx.application
import javafx.application

      

0


source







All Articles