简体   繁体   中英

Scala SWT project with SBT

How do you create Scala SWT project in SBT?

I know that you can use GIT repositories:

RootProject(uri("http://git.eclipse.org/gitroot/platform/eclipse.platform.swt.binaries.git"))

But I don't know how and if it is possible with SWT.

Thanks in advance, Etam.

EDIT :

I had to download it manually. It compiles but while running I get Invalid thread access error:

***WARNING: Display must be created on main thread due to Cocoa restrictions.
[error] (run-main) org.eclipse.swt.SWTException: Invalid thread access

Even if I use:

javaOptions := Seq("-XstartOnFirstThread", "-d64")

This is the main class:

import org.eclipse.swt._
import org.eclipse.swt.layout._
import org.eclipse.swt.widgets._

object Main extends App {
    val display = new Display
    val shell = new Shell(display)
    shell.setLayout(new GridLayout())
    shell.pack
    shell.open
    while (!shell.isDisposed) {
        if (!display.readAndDispatch)
            display.sleep
    }
    display.dispose
}

Thanks again, Etam.

Add this to your build.sbt :

resolvers += "swt-repo" at "http://maven-eclipse.github.io/maven"

libraryDependencies += {
  val os = (sys.props("os.name"), sys.props("os.arch")) match {
    case ("Linux", _) => "gtk.linux.x86"
    case ("Mac OS X", "amd64" | "x86_64") => "cocoa.macosx.x86_64"
    case ("Mac OS X", _) => "cocoa.macosx.x86"
    case (os, "amd64") if os.startsWith("Windows") => "win32.win32.x86_64"
    case (os, _) if os.startsWith("Windows") => "win32.win32.x86"
    case (os, arch) => sys.error("Cannot obtain lib for OS '" + os + "' and architecture '" + arch + "'")
  }
  val artifact = "org.eclipse.swt." + os
  "org.eclipse.swt" % artifact % "4.6.1"
}

It will first add a resolver for the SWT artifact repository. It will then detect your OS version and download an appropriate JAR for it.

As for the thread access problem, I solved this on Mac OS X by using JDK 1.6 with it - when I specify -XstartOnFirstThread there, it works fine. I've found no solution for JDK 1.7.

我认为最简单的方法是下载目标平台的SWT jar文件,把它放在lib/文件夹下,它会正常工作。

I encountered the same thread access error you did. It seems to come from sbt launching your application as sub-process in the same JVM as sbt itself is running.

I solved it by building a jar, and then running my application manually:

scala -classpath "target/scala-2.9.1/foobar-0.0.1.jar:lib/swt-debug.jar" -J"-XstartOnFirstThread" foo.bar.HelloWorld

There might be a more elegant solution, but this at least got me as far as being able to run my application.

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