Dima
Dima

Reputation: 27

scalaFX standalone execute jar file

Good day! Help me, please. I startup this example

sbt> run

It's okey all play, after

sbt> package

Will build jar file, after double click messge:

Error: A JNI error has occured, please check your installation and try again.

Scala version: 2.12.4. JVM:1.8.0_152. ScalaFX:8.0.102-R11

hello.scala: `

package hello

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.paint.Color._
import scalafx.scene.shape.Rectangle

object HelloStage extends JFXApp {

  stage = new JFXApp.PrimaryStage {
    title.value = "Hello Stage"
    width = 600
    height = 450
    scene = new Scene {
      fill = LightGreen
      content = new Rectangle {
        x = 25
        y = 40
        width = 100
        height = 100
        fill <== when(hover) choose Green otherwise Red
      }
    }
  }
}

build.sbt:

name := "Scala"

organization := "scalafx.org"

version := "1.0.5"

scalaVersion := "2.12.4"

scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")

resourceDirectory in Compile := (scalaSource in Compile).value

libraryDependencies ++= Seq(
  "org.scalafx" %% "scalafx" % "8.0.102-R11",)

addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)

fork := true

Upvotes: 3

Views: 586

Answers (1)

Mike Allen
Mike Allen

Reputation: 8279

This is a Java classpath issue. When you try to execute the resulting JAR file, it cannot find the jar files that it needs to run.

Try the following:

Firstly, copy & paste the following to project/plugins.sbt:

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.5")

This loads the sbt-assembly plugin, which will create a fat JAR file, containing all of the dependencies.

Secondly, change your build.sbt file to the following:

name := "Scala"

organization := "scalafx.org"

version := "1.0.5"

scalaVersion := "2.12.4"

scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xcheckinit", "-encoding", "utf8")

libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.102-R11"

fork := true

mainClass in assembly := Some("hello.HelloStage")

This simplifies what you originally had. The macro paradise compiler plugin is not required, and I also removed the slightly odd resourceDirectory setting.

To create the fat JAR, run the command:

sbt
sbt> assembly

The JAR file you're looking for is most likely located at target/scala-2.12/Scala-assembly-1.0.5.jar. You should now be good to go...

Alternatively, you can add all the necessary JAR files to your classpath. Another plugin that can help with that (you probably shouldn't use it with sbt-assembly) - is sbt-native-packager, which creates installers for you. You can then install your app and run it like a regular application.

Upvotes: 3

Related Questions