简体   繁体   中英

How do I run a basic ScalaFX application?

I'm trying to run a simple ScalaFX program that displays a window with nothing on it.

I can run the program as a script file with the desired result, but the moment I add a main object to the program it still runs but doesn't produce a window. I can compile the non-script file, but if I try to run it I get the following errors:

java.lang.NoClassDefFoundError: scalafx/application/JFXApp$PrimaryStage
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at scala.reflect.internal.util.ScalaClassLoader.run(ScalaClassLoader.scala:95)
        at scala.reflect.internal.util.ScalaClassLoader.run$(ScalaClassLoader.scala:91)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:125)
        at scala.tools.nsc.CommonRunner.run(ObjectRunner.scala:22)
        at scala.tools.nsc.CommonRunner.run$(ObjectRunner.scala:21)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
        at scala.tools.nsc.CommonRunner.runAndCatch(ObjectRunner.scala:29)
        at scala.tools.nsc.CommonRunner.runAndCatch$(ObjectRunner.scala:28)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:66)
        at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:85)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: java.lang.ClassNotFoundException: scalafx.application.JFXApp$PrimaryStage
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 19 more

What am I doing wrong?


    // Script file that works
    import scalafx.application.JFXApp

    val app = new JFXApp {
        stage = new JFXApp.PrimaryStage {
            title = "First GUI"
        }
    }

    app.main(args)


    // I run this in powershell with the following command:
    //scala -cp .\scalafx.jar .\program.scala


    // Program (non-script file) that doesn't give any errors when compiled but won't run. I can run this as a script file, but no window appears.

    import scalafx.application.JFXApp

    object Window extends JFXApp {
        stage = new JFXApp.PrimaryStage {
            title = "First GUI"
        }
    }

    // I compile this in powershell with the following command:
    //scalac -cp .\scalafx.jar .\program.scala
    // And run with this command:
    // scala Window

The compiled program generates the following names for class files (if that helps any):

Window$$anon$1.class, Window$.class, Window$delayedInit$body.class, Window.class,

The exception indicates that the JVM runtime was unable to locate the ScalaFX JAR file. (That is, the ScalaFX JAR file is not on the classpath when the program was run.)

Try using this command:

scala -cp .\scalafx.jar Window

UPDATE : I'm only guessing that this will work for you, given that you used the same classpath argument when you successfully ran the script and compiled the source file. (Note that all required JAR files must be present both at compile time and at run time.)

However, a more robust solution would be to use SBT to both build and run your application (the scripting use case isn't ideal for anything other than quick and dirty utility creation).

SBT , like Maven before it, imposes a structure upon your application's source files, which immediately makes the project comprehensible to any other SBT / Scala developer. It will also download and make available any dependent libraries, tools and plugins that you require. (There are a number of public and private artifact repositories , the primary one being the Maven central repository . SBT will happily use this, and you'll find ScalaFX available from there.)

To demonstrate how SBT simplifies your project development, all you need to do to run your application is to issue it the command (from your project's root directory):

sbt run <project-name>

It will download all required repositories, if it has not done so already, compile any changed sources, and run the application all in one step—and it will take care of the classpath for you.

There are even SBT plugins for creating native installers, so that you can install, run and uninstall your code like any commercial application.

You can find the SBT getting started guide through the link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM