Main extends scala.App is not working after upgraded to scala 2.11.0 to 2.12.0 - scala

I used the below code snippet (in scala-2.11.8) to make object as executable program. But when I migrated the scala version 2.12.0 it is not working. It is throwing error to implement some of the deprecated methods.
object Main extends App {
Console.println("Hello World ")
}
as there were a change in delayedInit method, How would I proceed to make my object as executable using App trait.
Q : How to make my object as executable in scala 2.12.0
Update : Facing this issue when Using Idea 14 with scala plugin
Working fine with scala REPL terminal

How are you running your snippet?
One behavior change is that it doesn't run as a script under 2.11 but does run under 2.12.
$ scala -nc ran.scala
Hello World
$ scala211 -nc ran.scala
$ cat ran.scala
object Main extends App {
Console.println("Hello World ")
}
Since that is the opposite of what you report, you're doing something totally different.

Related

java.lang.NoSuchMethodError in Scala SBT Shell after executing JAR [duplicate]

This question already has answers here:
How to run jar generated by package (possibly with other jars under lib)?
(3 answers)
Closed 2 years ago.
after packaging and executing a Scala application (build.sbt with version 2.12.0, but in fact having 2.13.3 installed) with SBT (version 1.3.13), I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.wrapRefArray([Ljava/lang/Object;)Lscala/collection/mutable/WrappedArray;
at org.example.GreetWorld$.printMessage(GreetWorld.scala:5)
The source file GreetWorld.scala that caused the error looks like this:
package org.example
object GreetWorld {
def printMessage(theMessage:String):Unit = {
println(s"${theMessage} from me")
}
}
The main file that is invoking the file above looks like this:
package org.example
object HelloWorld {
def main(args: Array[String]) = {
GreetWorld.printMessage("Hello")
}
}
Does anybody know the root cause? At first I thought it has to do with the SBT shell picking Java 11, but even after changing my Windows' JAVA_HOME to Java 8, I still get the same error. Compiling and running it in SBT Shell works fine. Only the JAR execution fails.
The error is pretty simple. When you run package you create a JAR which only contains the classes corresponding to your source code, nothing more. And your code depends on the Scala stdlib, so if you try to run it with java - jar it will fail with a class path error.
You have 4 solutions:
Run the JAR using scala directly. However, you need to use the same major version it was used to compile.
Put the Scala library jar in the classpath when running. This is basically the same as above. Thus again, you have to use the same major version.
Create an uber jar that already has the Scala stdlib (as well as any other dependency) in it, using sbt-assembly.
Create a native distributable that will set up everything for you, using sbt-native-packager.
For local development and testing option 1 is usually the best one.
For simple projects option 3 is, IMHO, the simplest alternative.
And for very complex projects option 4 is very popular.

Running multiple Scala apps from one jar on JVM

I have a Scala application that successfully runs on the JVM using an uber jar via the command: java -jar myapp.jar. I need to create a separate, but related Scala job that utilizes many of the same objects/functions/dependencies as the first, making it a great candidate to keep in the same code repository & uber jar. Please note that these Scala jobs do not utilize Spark, so spark2-submit is out of the equation.
Question: How can I run 2 separate Scala jobs from the same uber jar on the JVM? (I am using Scala 2.11.8 and SBT for jar assembly)
Additional Context:
I've already looked into related StackOverflow discussions, namely this post about specifying Java classes using java -cp myapp.jar MyClass and this post, which only presented the solution of running the Scala equivalent using scala -classpath myapp.jar MyClass.
While the scala -classpath solution may have worked for the OP of the second linked discussion, I'll be deploying my code to an environment that doesn't have executables for scala or sbt, only java.
Let's say these are the 2 Scala jobs I want to run:
// MyClass.scala
package mypackage
object MyClass {
def main(args: Array[String]): Unit = {
println("Hello, World!")
}
}
// MyClass2.scala
package mypackage
object MyClass2 {
def main(args: Array[String]): Unit = {
println("Hello, World! This is the second job!")
}
}
Is there a way to run Scala code using java -cp myapp.jar MyClass?
I've tried this and receive the following error:
Error: Could not find or load main class MyClass
The main alternative I can think of would be to create a Scala object that serves as a main entry point and takes a parameter to determine which job gets run. I'd like to avoid that solution if possible, but it would allow me to continue using java -jar myapp.jar, which has been working fine.
You need to use a fully qualified name for the App instance:
java -cp myapp.jar mypackage.MyClass

What is the difference between case object defined in scala repl and sbt console?

When I declare case object in SBT Console, it creates module
scala> case object A
defined module A
And when I define it in Scala REPL, it creates object
scala> case object A
defined object A
Thanks in advance!
There is no difference in using the Scala REPL or SBT Console.
The difference you are seeing is probably because of Scala Version.
Cross check the Scala Version on Scala REPL and SBT Console it will be different as in Scala with version 10 series case object is defined as module but after Scala version 11 series they show object.
You can check the Scala version when you are opening the SBT Console or Scala REPL
You can also refer to this.
Hope this clears your doubt.
Thanks

Scalatest deprecation warning

At work and at home I use Scala a lot recently. We are using Eclipse as IDE and ScalaTest 2.0 for testing.
Whenever I run a unit test, I get this message:
WARNING: -p has been deprecated and will be reused for a
different (but still very cool) purpose in ScalaTest 2.0.
Please change all uses of -p to -R.
My question is two-fold:
Why is this message appearing? I already have ScalaTest 2.0, so what is it trying to tell me?
Is it possible to get rid of this warning? Our policy is to keep code warning-free, as everyone should try to do. I could not find any information relating to this warning online or on StackOverflow.
For completeness, a code sample:
import org.scalatest.FunSuite
class WarningSpec extends FunSuite {
test("See if this gives a warning") {
assert(true)
}
}
That is likely an unfortunate case of the ScalaTest Eclipse plugin itself using the deprecated Runner command -p to set the run path when running tests. We'll try to reproduce that, and if that's the case, and fix it by using -R instead and update the plugin.

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.