简体   繁体   English

SBT无法编译HelloWorld ScalaFX示例,抱怨类路径中缺少javafx

[英]SBT won't compile HelloWorld ScalaFX example, complains about javafx missing from the classpath

I am new to Scala, and have never used a build tool before, and I am having some trouble getting the HelloWorld example on the scalafx quickstart page to compile. 我是Scala的新手,以前从未使用过构建工具,并且在scalafx快速入门页面上获取HelloWorld示例进行编译时遇到了一些麻烦。

I previously tried to use the scala IDE 4.7.0 Release to run the code, by creating a new scala project, and adding the scalafx.jar version 8.0.102-R11 as an external jar in the build path libraries' option. 我以前尝试通过创建一个新的scala项目,并在构建路径库的选项中将scalafx.jar版本8.0.102-R11添加为外部jar,来使用scala IDE 4.7.0版本来运行代码。 However, when I right clicked on the project and selected "Run as", there was no option to run it as a Scala Application, and trying to use the object name in a new run configuration didn't seem to work. 但是,当我右键单击该项目并选择“运行方式”时,没有选择将其作为Scala应用程序运行,并且尝试在新的运行配置中使用对象名称似乎不起作用。 I decided to try using the command line with SBT instead. 我决定尝试将命令行与SBT一起使用。

I am running Windows 10, and I have Java 8 installed, as well as JDK 12.0.2. 我正在运行Windows 10,并且安装了Java 8以及JDK 12.0.2。 I have created a project in my downloads folder, inside a folder called scalahelloworld, and ran the following command: 我在我的下载文件夹中的一个名为scalahelloworld的文件夹中创建了一个项目,并运行以下命令:

sbt new scala/hello-world.g8 sbt new scala / hello-world.g8

This created the hello world project normally, and it compiles and runs without issues. 这正常创建了hello world项目,并且编译和运行时没有问题。 However, when I tried following the instructions on the quick start guide for scalafx, adding the line 但是,当我尝试按照scalafx快速入门指南上的说明进行操作时,请添加以下行

libraryDependencies += "org.scalafx" %% "scalafx" % "8.0.144-R12" libraryDependencies + =“ org.scalafx” %%“ scalafx”%“ 8.0.144-R12”

to build.sbt, and well as using their example code in my main object, it gives me some compiling errors: build.sbt,并在我的主对象中使用其示例代码,它给了我一些编译错误:

C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld>sbt
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
[info] Loading project definition from C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\project
[info] Loading settings for project scalahelloworld from build.sbt ...
[info] Set current project to hello-world (in build file:/C:/Users/hugo.barroca/Downloads/SBT%20Project/scalahelloworld/)
[info] sbt server started at local:sbt-server-3c8f646db648b9d2b646
sbt:hello-world> compile
[info] Compiling 1 Scala source to C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\target\scala-2.12\classes ...
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:15:3: Symbol 'type javafx.event.EventTarget' is missing from the classpath.
[error] This symbol is required by 'class scalafx.stage.Window'.
[error] Make sure that type EventTarget is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Window.class' was compiled against an incompatible version of javafx.event.
[error]   stage = new PrimaryStage {
[error]   ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:17:17: Symbol 'term javafx.css' is missing from the classpath.
[error] This symbol is required by 'trait scalafx.css.Styleable'.
[error] Make sure that term css is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Styleable.class' was compiled against an incompatible version of javafx.
[error]     scene = new Scene {
[error]                 ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:17:5: Symbol 'type javafx.scene.Scene' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.Scene'.
[error] Make sure that type Scene is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Scene.class' was compiled against an incompatible version of javafx.scene.
[error]     scene = new Scene {
[error]     ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:22:15: Symbol 'type javafx.scene.text.Text' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.text.Text'.
[error] Make sure that type Text is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Text.class' was compiled against an incompatible version of javafx.scene.text.
[error]           new Text {
[error]               ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:25:24: Symbol 'term javafx.scene.paint' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.paint.LinearGradient'.
[error] Make sure that term paint is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'LinearGradient.class' was compiled against an incompatible version of javafx.scene.
[error]             fill = new LinearGradient(
[error]                        ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:36:26: Symbol 'type javafx.scene.effect.DropShadow' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.effect.DropShadow'.
[error] Make sure that type DropShadow is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'DropShadow.class' was compiled against an incompatible version of javafx.scene.effect.
[error]             effect = new DropShadow {
[error]                          ^
[error] 6 errors found
[error] (Compile / compileIncremental) Compilation failed
sbt:hello-world[error] Total time: 4 s, completed 23/08/2019, 13:19:55
>
sbt:hello-world>

I have come across some SO pages which led me to believe the problem might be either with my version of Java or JDK , or with the classpath. 我遇到过一些SO页面,这使我相信问题可能出在我的Java或JDK版本中 ,还是在类路径中。 However, I haven't been able to solve it so far. 但是,到目前为止,我还无法解决。 I have tried reinstalling Java, the JDK, STB and tried different versions of the ScalaFX jar, in particular version 8.0.12. 我尝试过重新安装Java,JDK,STB,并尝试过不同版本的ScalaFX jar,特别是8.0.12版本。 I have also tried installing openjfx following this post . 我也尝试在这篇文章之后安装openjfx。 So far there's been no changes. 到目前为止,还没有任何变化。

The only code I have is that of the example file: 我唯一的代码是示例文件的代码:

package hello

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.effect.DropShadow
import scalafx.scene.layout.HBox
import scalafx.scene.paint.Color._
import scalafx.scene.paint.{Stops, LinearGradient}
import scalafx.scene.text.Text

object Main extends JFXApp {

  stage = new PrimaryStage {
    title = "ScalaFX Hello World"
    scene = new Scene {
      fill = Black
      content = new HBox {
        padding = Insets(20)
        children = Seq(
          new Text {
            text = "Hello "
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(PaleGreen, SeaGreen))
          },
          new Text {
            text = "World!!!"
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(Cyan, DodgerBlue)
            )
            effect = new DropShadow {
              color = DodgerBlue
              radius = 25
              spread = 0.25
            }
          }
        )
      }
    }
  }
}

How would I go about solving this particular issue, so that the code compiles properly and displays the HelloWorld scalafx window? 我将如何解决这个特定问题,以便代码正确编译并显示HelloWorld scalafx窗口?

A bit of digging shows that you most likely need to add a OS-specific dependency for the JavaFX binaries. 一点点挖掘表明您很可能需要为JavaFX二进制文件添加特定于操作系统的依赖项。 Add the following to the bottom of your build.sbt 将以下内容添加到build.sbt的底部

lazy val osName = System.getProperty("os.name") match {
  case n if n.startsWith("Linux") => "linux"
  case n if n.startsWith("Mac") => "mac"
  case n if n.startsWith("Windows") => "win"
  case _ => throw new Exception("Unknown platform!")
}

// Add JavaFX dependencies
lazy val javaFXModules = Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
libraryDependencies ++= javaFXModules.map( m=>
  "org.openjfx" % s"javafx-$m" % "11" classifier osName
)

As they do in the example project: https://github.com/scalafx/scalafx-hello-world/blob/master/build.sbt 就像他们在示例项目中所做的那样: https : //github.com/scalafx/scalafx-hello-world/blob/master/build.sbt

It can also be an issue with some of the JDK distrubutions, such as the OpenJDK distribution on Ubuntu. 对于某些JDK发行版(例如Ubuntu上的OpenJDK发行版),这也可能是一个问题。 If I recall right there are two different ways to solve it. 如果我没记错的话,有两种不同的解决方法。 One is to add JavaFX to your dependencies: 一种是将JavaFX添加到您的依赖项中:

libraryDependencies += "org.openjfx" % "javafx" % "12.0.2" pomOnly()

If I remember right this is probably the nicest way to do it. 如果我没记错的话,这可能是最好的方法。 Should that work add it through your package manager, in Ubuntu that would be: 如果该工作通过您的软件包管理器添加,在Ubuntu中将是:

sudo apt install openjdk-8-jdk openjfx

https://mvnrepository.com/artifact/org.openjfx/javafx and https://askubuntu.com/questions/1091157/javafx-missing-ubuntu-18-04 might be useful. https://mvnrepository.com/artifact/org.openjfx/javafxhttps://askubuntu.com/questions/1091157/javafx-missing-ubuntu-18-04可能会有用。

Also as the askubuntu points out, you probably want to make sure you set it to run using Java 8 for your projects. 另外,正如askubuntu指出的那样,您可能要确保将其设置为在项目中使用Java 8运行。

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

相关问题 Ant Ivy 不会设置编译类路径 - Ant Ivy won't set compile classpath IntelliJ不会编译从文档复制的基本JavaFX程序 - IntelliJ won't compile basic JavaFX program copied from documentation 这是我教科书中的示例代码。 它不会编译 - This is an example code from my textbook. It won't compile 从HelloWorld示例ArcGIS android SDK中看不到地图 - Can't see the map from the HelloWorld example ArcGIS android SDK Java将无法使用RequestDispatcher进行编译,并且正确设置了类路径 - java won't compile with RequestDispatcher, classpath set correctly 如何在不具有Linux的IDE的情况下从Sphinx4编译并运行HelloWorld.java示例? - How to compile and run HelloWorld.java example from Sphinx4 without and IDE in Linux? Web套接字:浏览器不会收到该消息,抱怨它不是以0x00(字节)开头 - Web Sockets: Browser won't receive the message, complains about it not starting with 0x00 (byte) Jacoco 抱怨不太可能缺少分支覆盖 - Jacoco complains about unlikely missing branch coverage 编译后无法启动将JFoenix JavaFX Element添加到FXML - Adding JFoenix JavaFX Element to FXML won't launch after compile ScalaFX(或 JavaFX)- 多次从另一个主 class 调用 GUI - ScalaFX (or JavaFX) - Calling GUI from another main class multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM