Unable to run a standlone scala file - scala

I'm following examples out of Programming in Scala book by Odersky and I'm unable to run a simple standalone scala file with scala hello.scala. When I do this, I get this error:
Error: Could not find or load main class hello.scala
Caused by: java.lang.ClassNotFoundException: hello.scala
I'm using scala3.
Here's a state of my file and commands I'm running.

If you follow the steps from the documentation you'll see that you need to compile your file before running it.
If you run:
scalac hello.scala
scala hello
You should get the result you want.

you might consider using the new (and excellent) tool from virtuslab called 'scala-cli': https://scala-cli.virtuslab.org/

Related

Scala IDE is not working - scala could not find or load main class

I try to run a simple scala spark program but somehow its throwing me a scala could not find load main class error. Any 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.

Pig-Scala UDF exception- ScalaObject not found

I have a scala .class file that I convert to a jar and try to register to a pig script. It is able to find that class now BUT it throws a ClassNotFoundException for scala.ScalaObject.
I notice that there is a scala.ScalaObject.class entry in the scala-library jar in the littlepiggy/lib folder.
Question 1
Shouldn't this jar be directly accessible anyways? Or do I have to add this path to an equivalent of a CLASSPATH for Pig?
Question 2
After this, I forcefully registered that jar as well.
I got this error:
java.lang.NoSuchMethodError: scala.collection.JavaConversions$.asScalaIterator(Ljava/util/Iterator;)Lscala/collection/Iterator
This doesn't look right to me. Any ideas?
PS - This source suggests that I should include the scala-library jar but Pig should already be able to find it and anyways, its not really working for me.
(http://mehack.com/levenshtein-distance-function-for-pig-and-had-0)
The answer's here. Should have checked out a more exhaustive set of keywords.
NoSuchMethodError when attempting to implicitly convert a java to scala collection
I was using different scala versions to build the class file and then in pig.
Thanks!

Scala 2.9 won't run HelloWorld on Ubuntu 12.04

This one should be simple, but I can't figure it out myself.
I have Scala 2.9.1 installed on an Ubuntu 12.04 system.
The file is helloworld.scala:
object HelloWorld{
def main(args:Array[String]){
println("Hello, World!")
}
}
scalac helloworld works totally fine without issue. Then scala HelloWorld gives
Exception in thread "main" java.lang.RuntimeException: Cannot figure out how to run target: HelloWorld
at scala.sys.package$.error(package.scala:27)
at scala.tools.nsc.GenericRunnerCommand.scala$tools$nsc$GenericRunnerCommand$$guessHowToRun(GenericRunnerCommand.scala:38)
at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
at scala.tools.nsc.GenericRunnerCommand$$anonfun$2.apply(GenericRunnerCommand.scala:48)
at scala.Option.getOrElse(Option.scala:108)
at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:48)
at scala.tools.nsc.GenericRunnerCommand.<init>(GenericRunnerCommand.scala:17)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:33)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
I've seen this question several times on forums but the poster is usually just mixing the class name and the file name so they would be trying scala helloworld, this is not what I'm doing (right?) but I get the same error.
Edit
scala -classpath ./ HelloWorld works fine. So it's a classpath problem.
I tried this and got exactly the same. Then I cleared my classpath thus:
$ CLASSPATH=
and all worked.
Not what you asked, but since you have many dependencies, it would be better to use sbt and let it worry about classpaths.
I'm also using Ubuntu and testing with your example I can confirm that:
scala HelloWorld.scala
will run the file successfully as a Scala script. And:
scalac HelloWorld.scala
will create the class files.
But, running:
scala HelloWorld.class
will give the error you are getting.
However, if you are running
scala HelloWorld
in the directory that has the class file in it, then the program should execute without a problem.

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.