Search code examples
scalagradlescala-idescala-swing

Cannot add scala swing dependency via Gradle in Scala IDE


I am a beginner learning Scala. I am on Windows 8.1, using Scala IDE 4.0.0 and Gradle with scala and eclipse plugins to create a project, in which I want to use scala.swing and javax.media packages. I add both libraries as dependencies in build.gradle, only javax.media works.

The steps I have done are:

  1. create folders and files as follows

    foo
     |-src
        |-main
           |-scala
               |-Foo.scala
     |-build.gradle
    

    where build.gradle is Gradle build script and Foo.scala is a Scala script, as shown below

    <<build.gradle>>

    apply plugin: 'scala'
    apply plugin: 'eclipse'
    
    repositories {
      mavenCentral()
    }
    
    dependencies {
      compile 'org.scala-lang:scala-swing:2.10.4'
      compile 'javax.media:jmf:2.1.1e'
    }
    

    <<Foo.scala>>

    import javax.media.Player //no error
    import scala.swing._ //error: object swing is not a member of package scala
    
    class Foo {
    }
    
  2. using command line, navigate to foo and run gradle eclipse

  3. in Scala IDE, do import -> General -> Existing Projects into Workspace -> Browse -> Foo

Then, I get an error at Foo.scala, line 2, which reads object swing is not a member of package scala. And the Referenced Libraries list on the left panel shows only jmf-2.1.1e.jar for javax.media, as shown in the picture below

object swing is not a member of package scala

<<.classpath>>

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" path="src/main/scala"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER" exported="true"/>
<classpathentry kind="lib" path="C:/Users/mm/.gradle/caches/modules-2/files-2.1/javax.media/jmf/2.1.1e/fe9287a362578bfb8b7b9dba42af0ec80a297abb/jmf-2.1.1e.jar" exported="true"/>
</classpath>

I have tried to configure Eclipse's build path and add scala-swing.jar manually, which works okay, but then I have to do this every time I run the Gradle build script. Please suggest why scala-swing-something.jar did not get imported by Gradle, and how to fix it.


This is the logging message after running $ gradle dependencies:

$ gradle dependencies
:dependencies

------------------------------------------------------------
Root project
------------------------------------------------------------

archives - Configuration for archive artifacts.
No dependencies

compile - Compile classpath for source set 'main'.
+--- org.scala-lang:scala-swing:2.10.4
|    \--- org.scala-lang:scala-library:2.10.4
\--- javax.media:jmf:2.1.1e

default - Configuration for default artifacts.
+--- org.scala-lang:scala-swing:2.10.4
|    \--- org.scala-lang:scala-library:2.10.4
\--- javax.media:jmf:2.1.1e

runtime - Runtime classpath for source set 'main'.
+--- org.scala-lang:scala-swing:2.10.4
|    \--- org.scala-lang:scala-library:2.10.4
\--- javax.media:jmf:2.1.1e

testCompile - Compile classpath for source set 'test'.
+--- org.scala-lang:scala-swing:2.10.4
|    \--- org.scala-lang:scala-library:2.10.4
\--- javax.media:jmf:2.1.1e

testRuntime - Runtime classpath for source set 'test'.
+--- org.scala-lang:scala-swing:2.10.4
|    \--- org.scala-lang:scala-library:2.10.4
\--- javax.media:jmf:2.1.1e

zinc - The Zinc incremental compiler to be used for this Scala project.
No dependencies

BUILD SUCCESSFUL

Total time: 11.159 secs

Solution

  • I had the same issue. I think it was an incompatibility between the Scala version and the Scala Swing version. I had these dependencies:

    dependencies {
        compile 'org.scala-lang:scala-library:2.11.5'
        compile 'org.scala-lang:scala-swing:2.10.4'
    }
    

    To solve this, add the scala-library-all dependency instead:

    dependencies {
        compile 'org.scala-lang:scala-library-all:2.11.5'
    }
    

    You can leave it at that, but it will add all sorts of dependencies that you might not need. If you don't want that, invoke:

    gradle dependencies
    

    It will tell you what dependencies Gradle has found. Mine said, among other things:

    compile - Compile classpath for source set 'main'.
    +--- org.scala-lang:scala-library-all:2.11.5
    |    +--- org.scala-lang:scala-library:2.11.5
    |    +--- org.scala-lang.modules:scala-swing_2.11:1.0.1
    |    |    \--- org.scala-lang:scala-library:2.11.0 -> 2.11.5
    

    Note the Scala Swing dependency, which has a different name and version from the one I had specified. Copy-and-paste that into your build.gradle dependencies:

    dependencies {
        compile 'org.scala-lang:scala-library:2.11.5'
        compile 'org.scala-lang.modules:scala-swing_2.11:1.0.1'
    }
    

    And voilà:

    gradle cleanEclipse eclipse
    

    All dependencies work!