1

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
asinkxcoswt
  • 2,252
  • 5
  • 29
  • 57

2 Answers2

3

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!

Daniel A.A. Pelsmaeker
  • 47,471
  • 20
  • 111
  • 157
0

This looks like Gradle couldn't resolve the org.scala-lang:scala-swing:2.10.4 dependency when it generated the Eclipse files. I use IntelliJ and it does the same thing- everything looks to have worked ok, but dependencies are missing- very frustrating.

You can check by running

$ gradle dependencies

And checking that scala-swing is being resolved correctly. I checked Maven central and you definitely have the dependency listed correctly. If this shows no errors try cleaning your project files and regenerating.

tddmonkey
  • 20,798
  • 10
  • 58
  • 67
  • Thank you, I have tried as you suggest but I don't understand the logging message after running `$gradle dependencies` I updated the message under the question. – asinkxcoswt Jan 01 '15 at 13:58
  • Your dependencies look fine- if any couldn't be resolved it would say "FAILED" next to that dependency. Have you tried doing a gradle cleanEclipse eclipse? – tddmonkey Jan 01 '15 at 14:04
  • Yes, but still not working. I have tried `$ gradle cleanEclipse eclipse` -> refresh project, and also tried the `Project -> Clean` functionality of Eclipse. – asinkxcoswt Jan 01 '15 at 14:09
  • Have you tried closing Eclipse, running the gradle commands and then reopening? I've seen the same issue with IntelliJ sometimes and it ends up being related to the IDE saving some of its files before shutdown – tddmonkey Jan 01 '15 at 14:11
  • I tried that, and the error is still there (T_T). I also run the command and looked at .classpath with Eclipse closing, but no entry to scala-swing appear. – asinkxcoswt Jan 01 '15 at 14:19