Unable to run a HelloWorld Scala program on Ubuntu - scala

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.

Related

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

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.

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.

Does Scala having an interpreter give Scala projects the option to execute them either compiled or interpreted? [duplicate]

This question already has answers here:
The difference between scala script and application
(3 answers)
Closed 8 years ago.
I'm aware that scala has an interpreter and scala is statically typed. So I'm wondering if it is possible to execute scala projects in both Java and PHP style ?
Maybe you just need an interpreter to test your code? Then type scala to get interpreter and use :load command to load scala file.
I don't know exactly what is the PHP style, but yes, you can execute scala interactively, static typing is not a big issue here. If you need to exectue simple script that's easy (code from "Getting started in Scala"):
#!/bin/sh
exec scala "$0" "$#"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld.main(args)
If you have .jar dependencies in your script than things got more complicated,because you need to pass this jar dependencies to the scala interpreter. here is example
As of current date this method doesn't allow you to modularize scripts into the multiple files, but here is workaround
If you have sbt project you can type console from sbt shell to get a scala interpreter with correct classpath and dependencies. Also sbt itself has a 'script' mode which works quite like groovy's embedded dependency menegment.
Also scala compiler is embeddable) This project helps to dynamically compile/recompile scala files and load them into the jvm.

Scala question about examples in "Programming Scala" book

I've been reading this free online book and I'm hitting my head against a brick wall at the following section: -
http://programming-scala.labs.oreilly.com/ch01.html#ATasteOfConcurrency
At the end it tells you to run the following commands
scalac shapes.scala shapes-actor.scala
scala -cp . shapes-actor-script.scala
Except when I run the last command I just get this error
shapes-actor-script.scala:3: error: not found: value shapes
import shapes._
At first I just typed out the code, but then figuring I may have made a typo I downloaded the code examples and it does the same there.
I'm running the latest version of Scala on Java 1.6
Any replies would be appreciated.
Do this instead:
scala -cp $PWD shapes-actor-script.scala
Or maybe $PWD/. On Unix, anyway. Alternatively, try this:
scala -nocompdaemon -cp . shapes-actor-script.scala
The reason for this is that scala calls a daemon to run scripts, so any relative class paths are resolved against the directory on which the daemon was started. Tested on Scala 2.8.x, though I hope this changes in the future.

Scala : trying to get log4j working

Scala newb here (it's my 2nd day of using it). I want to get log4j logging working in my Scala script. The script and the results are below, any ideas as to what's going wrong?
[sean#ibmp2 pybackup]$ cat backup.scala
import org.apache.log4j._
val log = LogFactory.getLog()
log.info("started backup")
[sean#ibmp2 pybackup]$ scala -cp log4j-1.2.16.jar:. backup.scala
/home/sean/projects/personal/pybackup/backup.scala:1: error: value apache is not a member of package org
import org.apache.log4j._
^
one error found
I reproduce it under Windows: delimiter of '-classpath' must be ';' there (not ':'). Are you use cygwin or some sort of unix emulator?
But Scala script works anywhere without current dir in classpath. Try to use:
$ scala -cp log4j-1.2.16.jar backup.scala
JFI: LogFactory is a class of slf4j library (not log4j).
UPDATE
Another possible case: broken jar in classpath, maybe during download or something else. Scala interpreter does report only about unavailable member of the package.
$ echo "qwerty" > example.jar
$ scala -cp example.jar backup.scala
backup.scala:1: error: value apache is not a member of package org
...
Need to inspect content of the jar-file:
$ jar -tf log4j-1.2.16.jar
...
org/apache/log4j/Appender.class
...
Did you remember to put log4j.jar in your classpath?
Had Similar issue when started doing Scala Development using Eclipse, doing a clean build solved the problem.
Guess the Scala tools are not matured et.
Instead of using log4j directly, you might try using Configgy. It's the Scala Way™ to work with log4j, as well as configuration files. It also plays nicely with SBT and Maven.
I asked and answered this question myself, have a look:
Put it under src/main/resources/logback.xml. It will be copied to the right location when SBT is doing the artifact assembly.