Newbie flustered by "scala-swing"

Hello,
I am using fedora 38, Scala 3.0.0, and scala-swing 3.0.0 (from https://index.scala-lang.org)
My SCALA_HOME env var points at scala.
My CLASSPATH env points at scala-swing.jar
My JAVA_HOME env points at /usr/lib/jvm/openlogic-openjdk-17-hotpsot

I am trying to follow the simple Scala tutorial on youtube by Mark Lewis
I have simplified HelloWorld.scala to the following three lines …

import scala.swing._
frame = MainFrame(){}
frame.visible=true

… compiling in the same directory as HelloWorld.scala:
scalac HelloWorld.scala

… results with this error:
– [E008] Not Found Error: HW.scala:1:13
1 |import scala.swing._
** | ^^^^^^^^^^^**
** | value swing is not a member of scala - did you mean scala.Long?**

(Also tried copying scala-swing.jar to current directory and passing “-classpath .” to scalac but this does not work either)
Can anyone help? Can anyone get the three lines above to compile?
JohnP

Welcome to the Scala community @JohnP

Can you link to the tutorial video you mentioned?
I’m guessing it uses SBT?
Is there a code repository related to the video you can share? Or maybe you could try https://scastie.scala-lang.org/ to share the issue?

Looking at Mark’s youtube channel, it appears that his videos are some years old and predate Scala 3. Your choices are:

  1. find newer learning materials (perhaps Books | Scala Documentation or Online Courses (MOOCs) from The Scala Center | Scala Documentation or Rock the JVM videos)
  2. downgrade your Scala to whatever Mark was using in his videos
  3. muddle through, making adjustments as needed for all the ways the language and the tooling for it have changed

I don’t recommend option 3 as you’re likely to hit lots of annoying and distracting little obstacles. But if you go that route, I suspect the problem you’re encountering here is that scala-swing used to be part of the Scala standard library, but then later it became a separate library instead. So to use it with a recent Scala version such as 2.13 or 3, you’ll need to add it as a dependency. How to do that depends on what build tool and/or IDE you are using.

I would have to play with it, but the Java version might also be important.

Hi SpamEgg1
Thanks for your response. No, the tutorial is old but very simple and just uses scalac to compile. It does not link to SBT.
The tutorial is at https://www.youtube.com/watch?v=AbBvRtNBDL0
I know it is old but the documentation at scala-swing seems to indicate that in recent years the library has been maintained and made compatible with Scala 3.
My problem is why can’t scalac recognise the library, given the classpath is correct? Also the problem shows up on Scatsie too. So it should be easy to reproduce (I am not sure how I share issues on Scatsie nor do I know how to setup classpath on Scatsie)
JohnP :slight_smile:

Hi Seth,
Thanks for the detailed answer. I am interested to go for option 3. The reason is that the info at scala-swing indicates recent activity to create version 3.0.0 to match version 3 of Scala,
For the moment, to keep things simple, I just use scalac as my build tool, as in the original post. I have tried adding the line …

//>using dep org.scala-lang.modules::scala-swing:3.0.0

… at the top of the file and various mutations but I just cannot get scala-swing library to be recognized. Any help in this area would be much appreciated
JohnP

Scastie uses SBT under the hood.
Can you try Build settings, like so?

It seems to be working fine.
I think your issue is with the paths. I’ve never used bare-bones scalac myself so I can’t help there, I always used SBT, these days I’ve switched to Scala-cli

The using directives require Scala-cli, I don’t think they are recognized by scalac.

Can you try setting up a simple SBT or Scala-cli project? I’m sure that will fix the issue.

I should mention that almost nobody uses bare scalac, it’s not very easy to use. This is a common pain point for newcomers. Scala-cli was made to ease these issues.

Thanks Seth and SpamEgg1
Using the extra info you gave me I added the following line to the top of the file …
//> using dep org.scala-lang.modules::scala-swing:3.0.0
… and then compiled like this …
scala-cli compile HelloWorld.scala
… it gives me a class file in a directory below and then …
scala HelloWorld.class
… gives …
Error Could not find or load main class HelloWorld.class
… but this is now a different problem and I can research that tomorrow, along with setting up sbt
Thanks again for all your help
JohnP

I believe someone else was having a similar issue. The way scala-cli compiles stuff is not 1-1 compatible with / runnable by the old scala command. Instead you’ll have to stick with scala-cli and use scala-cli run ...

Just FYI, scala-cli will become the scala command in 3.4 and forward. (It’s kind of in a weird transition period right now with scala and scala-cli being two code-runners side-by-side.)

If you followed the setup at the main Scala website you should already have Sbt. You can setup a new Scala 3 project easily with: sbt new scala/scala3.g8 then add the dependency to the build.sbt file. Sbt does not understand using directives, it has different syntax which you can see on Scaladex

Correct, this is specific to scala-cli.

If scala-cli compile HelloWorld.scala works, then scala-cli run HelloWorld.scala should also work (the compilation will happen automatically as part of run).

scala-cli run HelloWorld.scala works for me on the following file:

//> using scala 3.3.1
//> using dep org.scala-lang.modules::scala-swing:3.0.0

@main def hello =
  import scala.swing.*
  val frame = MainFrame()
  frame.visible = true

It runs and opens a window.

1 Like

scala-cli run HelloWorld.scala works for me too thanks!

At first I tried to do the "scala-cli run " command on the .class file many directories below. That did not work! Your last post saved me. Thanks Again :slight_smile:

2 Likes

For that old-timey feeling, the class path must contain the jar:

scalac -cp scala-parallel-collections_2.13-1.0.4.jar -d /tmp/sandbox t11036.scala

scala-cli obviates the manual labor, but occasionally I must do it for a test.

Alternate syntax to mean “jars in the directory”:

scalac -cp './*' -d /tmp/sandbox t11036.scala

which IIRC is inherited from Java. The tool is doing that expansion, not the shell.

Wow, very old-timey indeed :laughing: I’ve been with Scala since 2020 and I’ve never used scalac, or tried to run a .class file, or use manual command line parameters… :woozy_face:

Thanks Everyone

2 Likes