Search code examples
sbtjava-8playframework-2.2javafx-8

Add JavaFX (of JDK8) in sbt (using the play framework)


The goal is to have a standalone Play Framework (2.2) application having an additional status window open containing some javafx (javafx-8) elements.

Since JavaFX classes are now on the default runtime classpath for an Oracle Java 8 implementation using javafx.* in my classes and compiling with sbt should just be fine.

However sbt can't find these classes and quits with

play.api.UnexpectedException: Unexpected exception[NoClassDefFoundError: javafx/application/Application]

when executing

..\path-to-play-framework-2.2\play project run

The best way to fix this problem seems to be the modification of build.sbt in the project directory. What can I do to add the missing (class) path?


Solution

  • Sadly JavaFX doesn't link that easily to an sbt build. You need to set your JAVA_HOME environment variable and do modifications to your build file.

    Here I have a repository where this is set up. The important bit if you are using an sbt build rather than a scala build is this one:

    unmanagedJars in Compile += Attributed.blank(
      file(System.getenv("JAVA_HOME") + "/jre/lib/jfxrt.jar")),
    
    fork in run := true
    

    The reason for this is that jfxrt.jar is the archive containing the JavaFX runtime and it is not included in the classpath of an sbt project by default.