Scala UDF: java.lang.NoClassDefFoundError: scala/ScalaObject - scala

I am trying to use Scala for UDFs, but the Pig job is failing with the
error "java.lang.NoClassDefFoundError: scala/ScalaObject". What am I doing wrong?
$ cat NonEmpty.scala
package nonempty
import org.apache.pig.FilterFunc
import org.apache.pig.data._
class NonEmpty extends FilterFunc {
def exec(input: Tuple) = {
val s = input.get(0)
s match {
case a: String => !a.isEmpty
case _ => false
}
}
}
$ cat ex3.pig
register ./nonempty.jar
register ./scala-library.jar;
define NonEmpty nonempty.NonEmpty();
raw = load 'excite-small.log' using PigStorage('\t') as (user: chararray, time:chararray, query: chararray);
locations = filter raw by NonEmpty(query);
Build:
scalac -cp ~/pig-0.9.2/pig-0.9.2.jar NonEmpty.scala
jar -cf nonempty.jar nonempty
Pig Stack Trace:
2 ---------------
3 ERROR 2998: Unhandled internal error. scala/ScalaObject
4
5 java.lang.NoClassDefFoundError: scala/ScalaObject
(...)

ScalaObject is located in the scala-library.jar which needs to be included on the runtime classpath. So add scala-library.jar to the runtime classpath of your command that is running the program.

Related

mill client.fastOpt: client.scalaJSLinkerClasspath scala.MatchError: 1 (of class java.lang.String)

I want to run a ScalaJS module with mill Build Tool.
When running mill client.fastOpt I get:
[6/73] client.scalaJSLinkerClasspath
1 targets failed
client.scalaJSLinkerClasspath scala.MatchError: 1 (of class java.lang.String)
mill.scalajslib.ScalaJSModule.$anonfun$scalaJSLinkerClasspath$2(ScalaJSModule.scala:38)
mill.define.ApplyerGenerated.$anonfun$zipMap$7(ApplicativeGenerated.scala:17)
mill.define.Task$MappedDest.evaluate(Task.scala:365)
My build.sc is:
trait BaseJsModule extends ScalaJSModule {
val scalaJSVersion = "1.0.1"
val scalaVersion = "2.13.1"
}
object client extends BaseJsModule {
override def moduleDeps = Seq(shared)
override def mainClass = Some("pme123.camunda.boot.client.HelloClient")
}
object shared extends BaseJsModule
Do I miss something?
Your posted buildfile looks ok. You're probably using a too old mill version? Support for ScalaJS 1.0.0+ was added in mill 0.6.1.
Please note, that you can create a file .mill-version with the content 0.6.1 to automatically download and use mill 0.6.1.

How to run Scala test in Scala native application?

I have hello world scala native app and wanted to run small scala test to this app I use the usual test command but it's throw an exception :
NativeMain.scala
object NativeMain {
val p = new Person("xxxx")
def main(args: Array[String]): Unit = {
println("Hello world")
}
}
class Person(var name: String)
}
NativeTest.scala
import org.scalatest.{FlatSpec, Matchers}
class NativeTest extends FlatSpec with Matchers {
"name" should "the name is set correctly in constructor" in {
assert(NativeMain.p.name == "xxxx")
}
}
I run test command in the sbt shell and got this error
[IJ]> test
[info] Compiling 1 Scala source to /home/****/Documents/ScalaNativeFresh/target/scala-2.11/classes...
[info] Compiling 1 Scala source to /home/****/Documents/ScalaNativeFresh/target/scala-2.11/test-classes...
[info] Compiling 1 Scala source to /home/****/Documents/ScalaNativeFresh/target/scala-2.11/test-classes...
[info] Linking (28516 ms)
[error] cannot link: #java.lang.Thread::getStackTrace_scala.scalanative.runtime.ObjectArray
[error] unable to link
[error] (nativetest:nativeLink) unable to link
[error] Total time: 117 s, completed Apr 2, 2019 3:04:24 PM
Any help or suggestions thank you :) ?
There is an open issue to add Add support for Scala Native #1112 and according to cheeseng:
3.1.0-SNAP6 and 3.2.0-SNAP10 are the only 2 versions (as of the time of writing) that supports scala-native
Try importing scalatest_native0.3_2.11 like so
libraryDependencies += "org.scalatest" % "scalatest_native0.3_2.11" % "3.2.0-SNAP10"
scalatest-native-example is a working example showing how to use scalatest with scala-native.

Scala akka actor system - RuntimeException when loading extension

I am new with scala and I face the following problem.
import akka.actor.Actor
import akka.actor.Props
import akka.event.Logging
import akka.actor.ActorSystem
object test extends App {
val system = ActorSystem("hello-world")
val myActor = system.actorOf(Props[MyActor], "myactor2")
}
class MyActor extends Actor {
val log = Logging(context.system, this)
val props1 = Props[MyActor]
def receive = {
case "test" => log.info("received test")
case _ => log.info("received unknown message")
}
val child = context.actorOf(Props[MyActor], name = "myChild")
}
So when I run it, I receive
[error] (run-main-1) java.lang.RuntimeException: While trying to load extension [akka.actor.InstanceCountingExtension]
java.lang.RuntimeException: While trying to load extension [akka.actor.InstanceCountingExtension]
at akka.actor.ActorSystemImpl.$anonfun$loadExtensions$1(ActorSystem.scala:906)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1406)
at scala.collection.IterableLike.foreach(IterableLike.scala:71)
...
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: java.lang.ClassNotFoundException: akka.actor.InstanceCountingExtension
at java.lang.ClassLoader.findClass(ClassLoader.java:530)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
The problem is the ActorSystem, but i can't understand why. When I delete the system an myActor everything works fine. It even works when i make it only val system = ActorSystem but then system couldn't create actorOf.
You probably have this setting in your application.conf
akka.library-extensions += "akka.actor.InstanceCountingExtension"
This is an actor system extension used by akka-actor-tests to make assertions about the number of actor instances currently running in a system.
I don't think this is intended as an extension to be used by applications, it is more like a test utility.
However, depending on what you need:
if you just want to run your program, just delete the configuration line above, and all should be fine
if you need the extension for some reason, import akka-actor-tests test jar, or alternatively make sure you got this class available at runtime.
The problem was with the test folder. When I removed the test it started working.

sbt: execute initialCommands silently

Is it possible to execute initialCommands in the console task silently, i.e. as if
:silent
val $session = new foo.bar.Session()
import $session._
import $session.lib._
:silent
Putting these commands in initialCommands doesn't work, though, because :<command> commands apparently cannot be used in initialCommands:
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions for evaluation. Or try :help.
scala> <console>:2: error: illegal start of definition
:silent
^
Interpreter encountered errors during initialization!
[error] (Thread-1) java.lang.InterruptedException
java.lang.InterruptedException
at java.util.concurrent.SynchronousQueue.put(SynchronousQueue.java:879)
at scala.tools.nsc.interpreter.SplashLoop.run(InteractiveReader.scala:77)
at java.lang.Thread.run(Thread.java:745)
Unfortunately, as of 0.13.13, sbt runs the initialCommands early, while it's creating the interpreter, and before the console has a chance to bind the interpreter as $intp.
This is close:
$ sbt -Dscala.repl.maxprintstring=-1
[info] Set current project to sbt-test (in build file:/home/apm/tmp/sbt-test/)
> console
[info] Starting scala interpreter...
[info]
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_111).
Type in expressions for evaluation. Or try :help.
scala> ...
scala> Future(42)
...
scala> $intp.isettings.max
maxAutoprintCompletion maxPrintString
scala> $intp.isettings.maxPrintString = 1000
$intp.isettings.maxPrintString: Int = 1000
scala> "hi"*1000
res0: String = hihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihihi...
scala> Future(42)
res1: scala.concurrent.Future[Int] = Future(Success(42))
It's a misfeature that setting maxPrintString to zero doesn't truncate everything, including the ellipsis, which is always residual.
I'm unaware of an sbt option to do that. In the lack of a better solution, you could hide all your setup in nice looking import as follows:
object console {
object setup {
val bar = foo.bar
bar.init()
}
}
Edit 1:
Note that this is equivalent to the code original code you wrote: it put a thing in scope called bar, which points to foo.bar. You can also use the same technique with types to group whatever imports you need into a single one. This is the mechanism used Predef to magically get scala.collection.immutable.Set (both the type and the value) in scope.
Edit 2:
I guess your technique can't achieve that with a single import.
It still works. Suppose Session is defined as follows:
trait Session {
val v
def f
lazy val l
object o {}
type T
}
then
val $session = new foo.bar.Session()
import $session._
becomes
object console {
object setup {
val $session = new foo.bar.Session()
val v = $session.v
def f = $session.f
lazy val l = $session.l
val o = $session.o
type T = $session.T
}
}
You can apply this transformation recursively for lib._ and whatever other imports you have until you've built the exact same scope.

Scala delimited continuations error at runtime

Scala newbie here, I just downloaded Eclipse 3.6.2 and Scala IDE 2.0.0-beta4 (with Scala 2.9.0.final). I create a new Scala project to try delimited continuations:
package delimCCTests
import scala.util.continuations._
object Test extends App {
val result = reset {
1 + shift { k: (Int => Int) => k(k(5)) } + 1
}
println(result)
}
This compiles fine, then I click Run as -> Scala application and get this exception:
Exception in thread "main" java.lang.NoSuchMethodError: scala.util.continuations.package$.shift(Lscala/Function1;)Ljava/lang/Object;
at delimCCTests.Test$$anonfun$1.apply$mcI$sp(DelimCCTests.scala:7)
at delimCCTests.Test$$anonfun$1.apply(DelimCCTests.scala:7)
at delimCCTests.Test$$anonfun$1.apply(DelimCCTests.scala:7)
at scala.util.continuations.package$.reset(package.scala:20)
at delimCCTests.Test$delayedInit$body.apply(DelimCCTests.scala:6)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.App$$anonfun$main$1.apply(App.scala:60)
at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
at scala.collection.immutable.List.foreach(List.scala:45)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
at scala.App$class.main(App.scala:60)
at delimCCTests.Test$.main(DelimCCTests.scala:5)
at delimCCTests.Test.main(DelimCCTests.scala)
What am I doing wrong? Am I missing some configuration?
BTW I thought the compiler inferred the type of the continuation? This article uses:
val result = reset {
1 + shift { k => k(k(5)) } + 1
}
but this doesn't compile in my environment...
This error means that you didn't add Scala CPS plugin - it's not a part of a standard assembly (so far). Put the jar on the classpath, and run Scala is follows, in order to have continuations enabled:
$ scala -P:continuations:enable
This can be solved in eclipse by adding the CPS plugins class on the Scala Compiler > Advanced section, as well as enabling the switch:
Xplugin should be scala.tools.selectivecps.SelectiveCPSPlugin and Xpluginsdir should be the dir which contains org.scala-lang.plugins.scala-continuations-plugin.jar