简体   繁体   English

如何运行基本的 ScalaFX 应用程序?

[英]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.我正在尝试运行一个简单的 ScalaFX 程序,该程序显示一个没有任何内容的窗口。

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, 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.该异常表明JVM 运行时无法找到ScalaFX JAR文件。 (That is, the ScalaFX JAR file is not on the classpath when the program was run.) (也就是说,当程序运行时, ScalaFX JAR文件不在类路径上。)

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.) (请注意,所有必需的JAR文件都必须在编译时运行时都存在。)

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来构建和运行您的应用程序(脚本用例对于快速和肮脏的实用程序创建以外的任何事情都不理想)。

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. SBT与之前的Maven一样,在您的应用程序的源文件上强加了一个结构,这立即使任何其他SBT / Scala开发人员都能理解该项目。 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.) (有许多公共和私人工件储存器中,主要的一个是Maven的中央资料库。SBT会很乐意用这个,你会从那里找到可用ScalaFX。)

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如何简化项目开发,运行应用程序所需要做的就是向它发出命令(从项目的根目录):

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.甚至还有用于创建本机安装程序的SBT插件,以便您可以像任何商业应用程序一样安装、运行和卸载您的代码。

You can find the SBT getting started guide through the link.您可以通过链接找到SBT入门指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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