Running scala in cmd makes i look like I am missing 'build.sbt' - scala

I'm trying to run Scala in my command line.
I checked my java, went to the Scala website, downloaded and installed it, updated my environment variables.
So far the only thing different from guides online is that the folder where sbt is installed does not include a "lib" folder.
I then run sbt command in my prompt, and I get this message:
It looks like I'm missing a file called build.sbt, what is this? and do i need it?
Edit:
If I press 'continue' on the picture above, I get
sbt:scalaproj>
Which looks fine, but if i type some code, like this:
sbt:scalaproj> var a : Int = 12;
Then it returns errors:
[error] Expected ';'
[error] var a : Int = 12
What in the world is going wrong? can someone point me to a guide for writing Scala in the prompt that is not too old to work?

Let's first understand the terminology. Scala is the language you are writing. SBT is an acronym for Scala Build Tool. Both of them have REPL.
When you call sbt in the command line, you initiate the REPL of sbt. The commands you can run there, are all commands sbt supports. You can find here the common commands. For example, if you run compile, it will compile the build.sbt located at the directory where you called the sbt command. Anyway, Scala commands WILL NOT work here. Scala commands are not sbt commands.
In order to run Scala REPL, you need to type console in the sbt REPL. You can find here the Scala REPL documentation. Within the Scala REPL you can run Scala commands.
P.S.
You can find the Scala download page here.

Related

Compiling with scalac does not find sbt dependencies

I tried running my Scala code in VSCode editor. I am able to run my script via spark-submit command. But when I am trying with scalac to compile, I am getting:
.\src\main\scala\sample.scala:1: error: object apache is not a member of package org
import org.apache.spark.sql.{SQLContext,SparkSession}
I have already added respective library dependencies to build.sbt.
Have you tried running sbt compile?
Running scalac directly means you're compiling only one file, without the benefits of sbt and especially the dependencies that you have added in your build.sbt file.
In a sbt project, there's no reason to use scalac directly. This defeats the purpose of sbt.

How to setup different Scala versions on the same machine?

I want to follow the book on Scala[1] but it uses Scala 3 and I have Scala 2 installed. I want to use both the versions, something on the lines of python2 and python3.
I tried installing Scala3 on my local using the official source but I could only grasp the project-level working directory. The sbt prompt does not work like a REPL would and I can only open REPL using Scala 2 (I checked the version everytime).
How do I open the REPL of Scala3 given I cannot uninstall Scala2?
The sbt prompt does not work like a REPL
If you execute sbt console from within project directory it will drop you into REPL version corresponding to the project's scalaVersion. For example, executing sbt console within project created with sbt new lampepfl/dotty.g8 would start Scala 3 REPL.
but I could only grasp the project-level working directory
For system-wide installation first install coursier and then execute cs install scala3-repl. This will install Scala 3 REPL alongside the Scala 2 one. Now Scala 3 REPL can be started with scala3-repl command whilst Scala 2 REPL simply with scala command.

error: ';' expected but double literal found

I am learning Scala and I am using typesafe activator to run the programs. I have my first program in the below path <downloads>\typesafe-activator-1.3.7\activator-dist-1.3.7\simple scala project\scala-2.11\Simple_scala_project.scala
Below is the program content
object Simple_scala_project {
def main (args:String): Unit =
{ println("My first scala program") }
}
when I am running the below command in the command line I am getting the error
scala -cp <downloads>\typesafe-activator-1.3.7\activator-dist-1.3.7\simple scala project\scala-2.11 Simple_scala_project
:1: error: ';' expected but double literal found.
Please help me in this.
I noted a similar error when I inadvertently placed a line including "sbt.version=1.3.13" (in my case) in the wrong file. I realize now that it should have gone into a folder insider my overall project folder called "project", inside a file called "build.properties" (instead of in "build.sbt").
The take-away from this is that I don't think you experienced a compiler error at all, but an error owing to some files in the project setup. Better SBT documentation may be found here.
It's absolutely true that SBT isn't needed for very simple examples, and indeed offends the sensibilities of ones volunteering to learn a new language (soon to be improved, Scala 3.x, not a hundred other extraneous technologies. I find IntelliJ Idea Community to be a boost, here, since its Scala support is first-rate, and will create simple example projects for you in a variety of ways Installing Scala / Installing Scala plugin for Idea.

Unable to run a HelloWorld Scala program on Ubuntu

I have my very first Scala program, which is as simple as:
object HelloWorld{
def main(args: Array[String]){
println("Hello world!")
}
}
I then try to compile it like so:
$ scalac HelloWorld.scala
And it compiles without any error messages. When however I try to run it like so:
$ scala HelloWorld
I get an error message:
No such file or class on classpath: HelloWorld
To implement this, I followed this tutorial and to solve the emerged error, I followed this suggestion. However,
$ scala objects.HelloWorld
also does not work. I know many people will now start heavily voting down my question and asking questions - have you ever tried to read some books on it (Yes, I did. I've read Horstman book for beginners, but it does not contain any information on compiling programs under Ubuntu). Still, I hope someone could help.
(This was a comment before, and I rephrased it to a response.)
You've done everything right, except for the last step: Use the java command instead of the scala command.
scala is the Scala REPL. No separate run command is required for Scala code, because it compiles to regular Java bytecode.
So try: java HelloWorld
For more complex programs that make use of the Scala library however, you need to include the Scala runtime library in the classpath. So, on the long run, it is beneficiary to use a tool like SBT, as pointed out by #roterl in the comments.
The answer saying that the scala command is just for the REPL is incorrect. You can see from the man page entry for scala (http://www.scala-lang.org/files/archive/nightly/docs-2.10.2/manual/html/scala.html) that it is intended to be used in the same way as the java command with the added flexibility that it will run the REPL, scripts, or compiled applications.
As some of the comments have indicated, this is almost certainly a path issue, which means that it requires more information to diagnose. One thing you can check is whether the scalac command produced a .class file in your current directory. If that is in the directory where you are running scala then the comments about needing . in your classpath are almost certainly correct.

Compile single scala file with TypeSafe Activator

I have Activator installed. Which means I have a full SBT on my system. I don't want to create a brand new activator project. All I want to do is compile a single scala file as we used to do with the scalac command. How can I do this please? Thanks.
You go into the directory containing your scala file and type "sbt compile" on the command line.
To run the program, you type "sbt run"
see also
http://www.scala-sbt.org/0.13/tutorial/Hello.html