I would like to load some scala class at runtime, the class are presents in my scala project.
More precisely, in the root folder of my project, I have the TmpCaseClass.scala :
class TmpHBaseCaseClass(val adresse:String,val age:Int,val nom:String,val id:Int,val salaire:Float)
I would like to compile it and use it at run time using toolBox like this :
val tb = universe.runtimeMirror(getClass.getClassLoader).mkToolBox()
val clazz = tb.compile(tb.parse("./TmpCaseClass.scala"))().asInstanceOf[Class[_]]
val ctor = clazz.getDeclaredConstructors()(0)
val instance = ctor.newInstance()
But I encounter the follwing error :
Exception in thread "main" scala.tools.reflect.ToolBoxError:
reflective compilation has failed:
illegal start of definition at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.throwIfErrors(ToolBoxFactory.scala:316)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.parse(ToolBoxFactory.scala:291)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$parse$1.apply(ToolBoxFactory.scala:417)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$$anonfun$parse$1.apply(ToolBoxFactory.scala:414)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.liftedTree2$1(ToolBoxFactory.scala:355)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$withCompilerApi$.apply(ToolBoxFactory.scala:355)
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl.parse(ToolBoxFactory.scala:414)
at Main$.delayedEndpoint$Main$1(Main.scala:17) at
Main$delayedInit$body.apply(Main.scala:13) 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:76) at
scala.App$$anonfun$main$1.apply(App.scala:76) at
scala.collection.immutable.List.foreach(List.scala:381) at
scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76) at Main$.main(Main.scala:13)
at Main.main(Main.scala)
The line 17 (where the error happens) corresponds to :
val clazz = tb.compile(tb.parse("./TmpCaseClass.scala"))().asInstanceOf[Class[_]]
How could I get rid of this error ?
1) Try to parse file contents, instead of file name.
2) Also you wont be able to cast to Class, since compilation returns Unit. You might want to put another line like scala.reflect.classTag[TmpCaseClass].runtimeClass. You can also get tree (AST) as a result of tb.parse, if that's what you need.
Related
I have a Scala annotation with parameters, some with default values:
case class A(x: String, y: Option[String] = None, z: Option[Boolean] = None)
extends scala.annotation.StaticAnnotation
The goal is to annotate classes with this, and collect annotation parameters at runtime.
If I specify annotation parameters using positional notation, things work fine:
#A("xx", None, Some(true)) case class X()
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
val m = runtimeMirror(getClass.getClassLoader)
val t = m.mkToolBox()
val x = typeOf[X].typeSymbol.asClass.annotations.find(_.tree.tpe =:= typeOf[A]).get
println(t.eval(t.untypecheck(x.tree)).asInstanceOf[A])
This would print A(xx,None,Some(true)), as it should.
The following, however, would not work:
#A("x", z = Some(true)) case class Y()
val y = typeOf[Y].typeSymbol.asClass.annotations.find(_.tree.tpe =:= typeOf[A]).get
println(t.eval(t.untypecheck(y.tree)).asInstanceOf[A])
it would result in an exception I've never seen before:
java.lang.AssertionError: assertion failed: unsafe symbol x$3 (child of <none>) in runtime reflection universe
at scala.reflect.internal.Symbols$Symbol.<init>(Symbols.scala:205)
at scala.reflect.internal.Symbols$TermSymbol.<init>(Symbols.scala:2768)
at scala.reflect.internal.Symbols$StubTermSymbol.<init>(Symbols.scala:3524)
at scala.reflect.internal.Symbols$class.newStubSymbol(Symbols.scala:192)
at scala.reflect.internal.SymbolTable.newStubSymbol(SymbolTable.scala:16)
at scala.reflect.internal.Symbols$Symbol.newStubSymbol(Symbols.scala:521)
at scala.reflect.internal.pickling.UnPickler$Scan.readExtSymbol$1(UnPickler.scala:258)
at scala.reflect.internal.pickling.UnPickler$Scan.readSymbol(UnPickler.scala:286)
at scala.reflect.internal.pickling.UnPickler$Scan.readSymbolRef(UnPickler.scala:651)
at scala.reflect.internal.pickling.UnPickler$Scan.readNonEmptyTree(UnPickler.scala:610)
at scala.reflect.internal.pickling.UnPickler$Scan.readTree(UnPickler.scala:623)
at scala.reflect.internal.pickling.UnPickler$Scan$$anonfun$readAnnotArg$1.apply(UnPickler.scala:471)
at scala.reflect.internal.pickling.UnPickler$Scan$$anonfun$readAnnotArg$1.apply(UnPickler.scala:471)
at scala.reflect.internal.pickling.UnPickler$Scan.at(UnPickler.scala:179)
at scala.reflect.internal.pickling.UnPickler$Scan.readAnnotArg(UnPickler.scala:471)
at scala.reflect.internal.pickling.UnPickler$Scan.readAnnotationInfo(UnPickler.scala:505)
at scala.reflect.internal.pickling.UnPickler$Scan.readSymbolAnnotation(UnPickler.scala:517)
at scala.reflect.internal.pickling.UnPickler$Scan.run(UnPickler.scala:97)
at scala.reflect.internal.pickling.UnPickler.unpickle(UnPickler.scala:38)
at scala.reflect.runtime.JavaMirrors$JavaMirror.unpickleClass(JavaMirrors.scala:619)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply$mcV$sp(SymbolLoaders.scala:28)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply(SymbolLoaders.scala:25)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply(SymbolLoaders.scala:25)
at scala.reflect.internal.SymbolTable.slowButSafeEnteringPhaseNotLaterThan(SymbolTable.scala:263)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter.complete(SymbolLoaders.scala:25)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1535)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.scala$reflect$runtime$SynchronizedSymbols$SynchronizedSymbol$$super$info(SynchronizedSymbols.scala:168)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anonfun$info$1.apply(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anonfun$info$1.apply(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$class.gilSynchronizedIfNotThreadsafe(SynchronizedSymbols.scala:123)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.gilSynchronizedIfNotThreadsafe(SynchronizedSymbols.scala:168)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$class.info(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.info(SynchronizedSymbols.scala:168)
at scala.reflect.internal.Symbols$Symbol.initialize(Symbols.scala:1680)
at scala.reflect.internal.Symbols$Symbol.annotations(Symbols.scala:1847)
at test.TestAnnotation$.delayedEndpoint$test$TestAnnotation$1(TestAnnotation.scala:18)
at test.TestAnnotation$delayedInit$body.apply(TestAnnotation.scala:12)
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:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at test.TestAnnotation$.main(TestAnnotation.scala:12)
at test.TestAnnotation.main(TestAnnotation.scala)
Exception in thread "main" java.lang.RuntimeException: error reading Scala signature of test.Y: assertion failed: unsafe symbol x$3 (child of <none>) in runtime reflection universe
at scala.reflect.internal.pickling.UnPickler.unpickle(UnPickler.scala:46)
at scala.reflect.runtime.JavaMirrors$JavaMirror.unpickleClass(JavaMirrors.scala:619)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply$mcV$sp(SymbolLoaders.scala:28)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply(SymbolLoaders.scala:25)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter$$anonfun$complete$1.apply(SymbolLoaders.scala:25)
at scala.reflect.internal.SymbolTable.slowButSafeEnteringPhaseNotLaterThan(SymbolTable.scala:263)
at scala.reflect.runtime.SymbolLoaders$TopClassCompleter.complete(SymbolLoaders.scala:25)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1535)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.scala$reflect$runtime$SynchronizedSymbols$SynchronizedSymbol$$super$info(SynchronizedSymbols.scala:168)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anonfun$info$1.apply(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anonfun$info$1.apply(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$class.gilSynchronizedIfNotThreadsafe(SynchronizedSymbols.scala:123)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.gilSynchronizedIfNotThreadsafe(SynchronizedSymbols.scala:168)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$class.info(SynchronizedSymbols.scala:127)
at scala.reflect.runtime.SynchronizedSymbols$SynchronizedSymbol$$anon$8.info(SynchronizedSymbols.scala:168)
at scala.reflect.internal.Symbols$Symbol.initialize(Symbols.scala:1680)
at scala.reflect.internal.Symbols$Symbol.annotations(Symbols.scala:1847)
at test.TestAnnotation$.delayedEndpoint$test$TestAnnotation$1(TestAnnotation.scala:18)
at test.TestAnnotation$delayedInit$body.apply(TestAnnotation.scala:12)
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:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:392)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at test.TestAnnotation$.main(TestAnnotation.scala:12)
at test.TestAnnotation.main(TestAnnotation.scala)
As soon as I switch back to the positional arguments, things start working again.
Any idea on what am I facing here?
EDIT: Tried this with Scala 2.11 and 2.13, the failure and exception is consistent for both versions
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.
I am trying to get a map of name -> id from the resultset.
val isp = SQL("select id, name from internet_service_providers").map { x => x[String]("name") -> x[String]("id") }
I am unable to understand why I am getting this error.
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(Ljava/lang/Object;)Ljava/lang/Object;
at anorm.SqlStatementParser$$anonfun$3.apply(SqlStatementParser.scala:43)
at anorm.SqlStatementParser$$anonfun$3.apply(SqlStatementParser.scala:43)
at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:136)
at scala.util.parsing.combinator.Parsers$Success.map(Parsers.scala:135)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:242)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:242)
at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:242)
at scala.util.parsing.combinator.Parsers$Parser$$anonfun$map$1.apply(Parsers.scala:242)
at scala.util.parsing.combinator.Parsers$$anon$3.apply(Parsers.scala:222)
at scala.util.parsing.combinator.RegexParsers$class.parse(RegexParsers.scala:148)
at anorm.SqlStatementParser$.parse(SqlStatementParser.scala:11)
at anorm.SqlStatementParser$$anonfun$parse$1.apply(SqlStatementParser.scala:26)
at anorm.SqlStatementParser$$anonfun$parse$1.apply(SqlStatementParser.scala:26)
at scala.util.Try$.apply(Try.scala:161)
at anorm.SqlStatementParser$.parse(SqlStatementParser.scala:26)
at anorm.package$.SQL(package.scala:40)
at com.gumgum.nativead.NativeInventoryApp$.main(NativeInventoryApp.scala:49)
at com.gumgum.nativead.NativeInventoryApp.main(NativeInventoryApp.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
I am guessing that my way of creating the map in code above might be completely wrong or there is a scala version mismatch in the libs used.
I am using scala 2.11.5 and anrom 2.4.0-M3 built with scala 2.11
First the error java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(Ljava/lang/Object;)Ljava/lang/Object; is not from Anorm but from Predef: the -> operator is not found to build tupple, which is quite weird. I would suggest to check your scala version and dependencies, to be sure there is not several scala lib pulled.
Then if you want to turn a Row as a tuple, SqlParser.flatten can be used.
Finally as the result will be a list of tuple, .toMap can be used.
import anorm.SqlParser.{ flatten, str }
SQL("...").as((str("name") ~ str("id")).map(flatten).*).toMap
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.
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