I have an unmanagedClasspath entry in my lift sbt file that points to the classes for an external java project. It compiles fine but I get a NoClassDefError when it runs. Below is the sbt entry followed by some of the trace of the NoClassDefError.
Any help much appreciated
Des
unmanagedClasspath in Compile += file("[Path to my project]/classes")
ERROR n.liftweb.http.provider.HTTPProvider - Failed to Boot! Your application may not run properly
java.lang.NoClassDefFoundError: com/myproject/MyClass
at bootstrap.liftweb.Boot.boot(Boot.scala:70) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_21]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_21]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_21]
at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_21]
at net.liftweb.util.ClassHelpers$$anonfun$createInvoker$1.apply(ClassHelpers.scala:364) ~[lift-util_2.9.1-2.5-RC2.jar:2.5-RC2]
at net.liftweb.util.ClassHelpers$$anonfun$createInvoker$1.apply(ClassHelpers.scala:362) ~[lift-util_2.9.1-2.5-RC2.jar:2.5-RC2]
at net.liftweb.http.DefaultBootstrap$$anonfun$boot$1.apply(LiftRules.scala:1999) ~[lift-webkit_2.9.1-2.5-RC2.jar:2.5-RC2]
at net.liftweb.http.DefaultBootstrap$$anonfun$boot$1.apply(LiftRules.scala:1999) ~[lift-webkit_2.9.1-2.5-RC2.jar:2.5-RC2]
at net.liftweb.common.Full.map(Box.scala:553) ~[lift-common_2.9.1-2.5-RC2.jar:2.5-RC2]
unmanagedJars in Compile is used as base for the same key in Runtime and Test, so its content is inherited by Runtime, but this is not the case for unmanagedClasspath. That's why you should prefer unmanagedJars when possible.
You can explicitly add your folder to both configurations:
val additionalClasses = file("[Path to my project]/classes")
unmanagedClasspath in Compile += additionalClasses
unmanagedClasspath in Runtime += additionalClasses
Note: sbt < 0.13 doesn't support val in build.sbt, so you have to duplicate its content in both settings, or switch to a full Build.scala.
See the Classpaths docs and this anwser for more details on what the classpath settings do and how they are inherited between configurations.
I could not think of a way to add it to both at the same time though...
Related
I am trying to compile an old school project from around March, 2015. I haven’t touched it since then. The project is written in Scala with the Swing library, and the build tool is sbt.
Apparently my build.sbt file needs updating. After digging the Internet, I found that this configuration should work.
lazy val root = (project in file (".")).
settings (
name := "MyProgram",
/* FORMERLY: */
//libraryDependencies += "org.scala-lang" % "scala-swing" % "2.10+",
//ivyScala := ivyScala.value map { _.copy(overrideScalaVersion = true) }
/* NOW: */
scalaVersion := "2.13.0",
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "2.1.1"
)
The trick would be to use compatible versions of Scala and Swing. I tried the following pairs, which I expected to work together (either because I saw them on the webpage of package Swing, or because it worked for other people on SO):
(Scala 2.13.0, Swing 2.1.1)
(Scala 2.12.6, Swing 2.1.1)
(Scala 2.12.1, Swing 2.0.0-M)
(Scala 2.11.1, Swing 1.0.2)
I also tried switching from Java 12 (jdk-openjdk 12.0.2.u10-1) to Java 8 (jdk8-openjdk 8.u222-2).
However, all combinations give the same error. Compiling (sbt compile) works fine, but running the application (sbt run) results in a bunch of NoClassDefFoundError / ClassNotFoundException errors:
$ sbt run
[info] Loading project definition from /home/me/MyProject/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to MyProject (in build file:/home/me/MyProject/)
[info] Compiling 22 Scala sources to /home/me/MyProject/target/scala-2.12/classes ...
[warn] there were four deprecation warnings (since 2.12.0); re-run with -deprecation for details
[warn] one warning found
[info] running Main.Main
[error] java.lang.NoClassDefFoundError: scala/swing/event/WindowOpened
[error] at scala.swing.Window$$anon$1.windowOpened(Window.scala:80)
...
[error] Caused by: java.lang.ClassNotFoundException: scala.swing.event.WindowOpened
[error] at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
...
[error] java.lang.NoClassDefFoundError: scala/swing/event/WindowActivated
[error] at scala.swing.Window$$anon$1.windowActivated(Window.scala:74)
...
[error] Caused by: java.lang.ClassNotFoundException: scala.swing.event.WindowActivated
[error] at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
...
The application’s main window shows up but is pretty unusable, with every GUI element generating more errors when activated; even trying to close the window via the window manager fails.
What is missing to run this program?
I installed IntelliJ idea community edition 14.1.3 on my windows 7 machine.
I created a simple sbt project where the build.sbt file looks like
name := "SlickTest"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.0-RC3",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"com.typesafe" % "config" % "1.0.2"
)
Now I wrote a simple class
import com.typesafe.config.ConfigFactory
object Foo {
def main(args : Array[String]): Unit = {
println("foo")
val conf = ConfigFactory.load()
val foo = conf.getString("derby.url")
println(foo)
}
}
I created a file called "application.conf" in the resources folder.
derby = {
url = "foo"
}
Now if I go to command line and say sbt clean compile run I can see
D:\myapps\Scala\SlickTest>sbt clean compile run
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from D:\myapps\Scala\SlickTest\project
[info] Set current project to SlickTest (in build file:/D:/myapps/Scala/SlickTest/)
[success] Total time: 0 s, completed May 26, 2015 2:24:04 AM
[info] Updating {file:/D:/myapps/Scala/SlickTest/}slicktest...
[info] Resolving jline#jline;2.12.1 ...
[info] Done updating.
[info] Compiling 1 Scala source to D:\myapps\Scala\SlickTest\target\scala-2.11\classes...
[success] Total time: 3 s, completed May 26, 2015 2:24:07 AM
[info] Running Foo
foo
foo
[success] Total time: 0 s, completed May 26, 2015 2:24:07 AM
D:\myapps\Scala\SlickTest>
but when I try to build the same project in Intellij idea I see
Error:scalac: Error: Could not find an output directory for D:\myapps\Scala\SlickTest\src\main\scala-2.11\SlickTest.scala in List((D:\myapps\scala\SlickTest\src\test\resources,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\resources,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\scala-2.11,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\scala,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\java,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\scala-2.11,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\scala,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\java,D:\myapps\scala\SlickTest\target\scala-2.11\classes))
scala.reflect.internal.FatalError: Could not find an output directory for D:\myapps\Scala\SlickTest\src\main\scala-2.11\SlickTest.scala in List((D:\myapps\scala\SlickTest\src\test\resources,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\resources,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\scala-2.11,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\scala,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\test\java,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\scala-2.11,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\scala,D:\myapps\scala\SlickTest\target\scala-2.11\classes), (D:\myapps\scala\SlickTest\src\main\java,D:\myapps\scala\SlickTest\target\scala-2.11\classes))
at scala.tools.nsc.settings.MutableSettings$OutputDirs.outputDirFor(MutableSettings.scala:311)
at scala.tools.nsc.backend.jvm.BytecodeWriters$class.outputDirectory(BytecodeWriters.scala:26)
at scala.tools.nsc.backend.jvm.GenASM.outputDirectory(GenASM.scala:23)
at scala.tools.nsc.backend.jvm.BytecodeWriters$class.getFile(BytecodeWriters.scala:41)
at scala.tools.nsc.backend.jvm.GenASM.getFile(GenASM.scala:23)
at scala.tools.nsc.backend.jvm.GenASM$JBuilder.writeIfNotTooBig(GenASM.scala:531)
at scala.tools.nsc.backend.jvm.GenASM$JMirrorBuilder.genMirrorClass(GenASM.scala:2835)
at scala.tools.nsc.backend.jvm.GenASM$AsmPhase.emitFor$1(GenASM.scala:193)
at scala.tools.nsc.backend.jvm.GenASM$AsmPhase.run(GenASM.scala:203)
at scala.tools.nsc.Global$Run.compileUnitsInternal(Global.scala:1500)
at scala.tools.nsc.Global$Run.compileUnits(Global.scala:1487)
at scala.tools.nsc.Global$Run.compileSources(Global.scala:1482)
at scala.tools.nsc.Global$Run.compile(Global.scala:1580)
at xsbt.CachedCompiler0.run(CompilerInterface.scala:126)
at xsbt.CachedCompiler0.run(CompilerInterface.scala:102)
at xsbt.CompilerInterface.run(CompilerInterface.scala:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:102)
at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:48)
at sbt.compiler.AnalyzingCompiler.compile(AnalyzingCompiler.scala:41)
at org.jetbrains.jps.incremental.scala.local.IdeaIncrementalCompiler.compile(IdeaIncrementalCompiler.scala:29)
at org.jetbrains.jps.incremental.scala.local.LocalServer.compile(LocalServer.scala:26)
at org.jetbrains.jps.incremental.scala.remote.Main$.make(Main.scala:62)
at org.jetbrains.jps.incremental.scala.remote.Main$.nailMain(Main.scala:20)
at org.jetbrains.jps.incremental.scala.remote.Main.nailMain(Main.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.martiansoftware.nailgun.NGSession.run(NGSession.java:319)
Does anyone know why can't IntelliJ build the same project when sbt can easily build it.
I searched for couple of could not find output directory threads but they did not help me because unlike others I don't have a capitalization error in my path or project name.
Open Project settings and fill up the input with output path for this project, as on the image below.
I'm trying to create basic scala project in intellij by using the Activator UI
I'm importing the project to the ide and it compile well
But when im trying to run simple code im getting
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class
at akka.util.Collections$EmptyImmutableSeq$.<init>(Collections.scala:15)
at akka.util.Collections$EmptyImmutableSeq$.<clinit>(Collections.scala)
at akka.japi.Util$.immutableSeq(JavaAPI.scala:209)
at akka.actor.ActorSystem$Settings.<init>(ActorSystem.scala:150)
at akka.actor.ActorSystemImpl.<init>(ActorSystem.scala:470)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:111)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:104)
at reactivemongo.api.MongoDriver$.reactivemongo$api$MongoDriver$$defaultSystem(api.scala:378)
at reactivemongo.api.MongoDriver$$anonfun$3.apply(api.scala:305)
at reactivemongo.api.MongoDriver$$anonfun$3.apply(api.scala:305)
at scala.Option.getOrElse(Option.scala:120)
at reactivemongo.api.MongoDriver.<init>(api.scala:305)
at example.App$.main(App.scala:10)
at example.App.main(App.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)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
When the project is loaded there is an error in the project structure
sbt:scala 2.11.2 not in use
What went wrong with the activator ui intellij project generation ?
thanks
miki
I came across this when trying to run spark. This is a incompatibility error between the scala version which was used to compile the dependancy and the version of scala which is used to run your project.
Removing my scala version specification was a hacky way to solve the problem:
// build.sbt
name := "SparkTest"
version := "1.0"
scalaVersion := "2.11.4" <-- remove this
libraryDependencies += "org.apache.spark" % "spark-core_2.10" % "1.3.0"
Any classes defined in project/*.scala files are made available for use by SBT inside the build definition code.
I would like those classes to be available during the execution of an SBT plugin task, but they do not appear to be available.
Why is that, and how can I fix it?
The specific problem I am trying to solve is adding custom rules for Scalastyle. The project does not currently support writing your own rules, but I thought I might be able to add a rule to the project/*.sbt files and then use it inside Scalastyle.
If I define a rule in project/MyRule.scala, then it is available in the project/Build.scala settings:
object MyBuild extends Build {
lazy val project = Project("MyProject", file("."))
.settings(ScalastylePlugin.Settings: _*)
// my rule is available in this classloader:
val test = classOf[MyRule].getName
...
}
... but when the ScalastylePlugin Task runs, the classloader it is using cannot see that class:
java.lang.NoClassDefFoundError: MyRule
java.net.URLClassLoader$1.run(URLClassLoader.java:202)
java.security.AccessController.doPrivileged(Native Method)
java.net.URLClassLoader.findClass(URLClassLoader.java:190)
java.lang.ClassLoader.loadClass(ClassLoader.java:307)
java.lang.ClassLoader.loadClass(ClassLoader.java:248)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
org.scalastyle.Checker$.newInstance(Checker.scala:194)
org.scalastyle.Checker$$anonfun$verifySource0$1.apply(Checker.scala:116)
org.scalastyle.Checker$$anonfun$verifySource0$1.apply(Checker.scala:116)
...
org.scalastyle.ScalastyleChecker.checkFiles(Checker.scala:64)
org.scalastyle.sbt.Tasks$.runScalastyle(Plugin.scala:121)
org.scalastyle.sbt.Tasks$.doScalastyle(Plugin.scala:90)
org.scalastyle.sbt.ScalastylePlugin$$anonfun$4$$anonfun$apply$1.apply(Plugin.scala:63)
org.scalastyle.sbt.ScalastylePlugin$$anonfun$4$$anonfun$apply$1.apply(Plugin.scala:63)
scala.Function6$$anonfun$tupled$1.apply(Function6.scala:35)
scala.Function6$$anonfun$tupled$1.apply(Function6.scala:34)
scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
sbt.$tilde$greater$$anonfun$$u2219$1.apply(TypeFunctions.scala:42)
sbt.std.Transform$$anon$4.work(System.scala:64)
sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
sbt.Execute$$anonfun$submit$1$$anonfun$apply$1.apply(Execute.scala:237)
sbt.ErrorHandling$.wideConvert(ErrorHandling.scala:18)
sbt.Execute.work(Execute.scala:244)
sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
sbt.Execute$$anonfun$submit$1.apply(Execute.scala:237)
sbt.ConcurrentRestrictions$$anon$4$$anonfun$1.apply(ConcurrentRestrictions.scala:160)
sbt.CompletionService$$anon$2.call(CompletionService.scala:30)
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
...
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
java.lang.Thread.run(Thread.java:619)
I have inspected the classloader in use during the Scalastyle task, and it has all the SBT librarydependency jars on its classpath, but no classes from the SBT project.
How can I add these classes to its classpath?
I don't think it is possible, as documented in the SBT documentation
Note: At runtime, all plugins for all builds are loaded in a separate,
parent class loader of the class loaders for builds. This means that
plugins will not see classes or resources from build definitions.
Edited
Based on the comments, I think maybe this solution may work. In the project/ create extra project called scala-style-defs. There place your rules. In the same project create build.sbt with the build definition e.g.
libraryDependencies += "org.scalastyle" %% "scalastyle" % "0.4.0"
resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"
Then in the project/ create build.sbt with following content
lazy val root = project.in(file(".")) dependsOn(scalastyleDefs)
lazy val scalastyleDefs = Project(id="scalastyleDefs", base=file("scala-style-defs"))
and of course plugins.sbt with
addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "0.4.0")
resolvers += "sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases/"
Now in your main project create build.sbt and there include scalastyle settings
org.scalastyle.sbt.ScalastylePlugin.Settings
I'm building a Scala project with sbt and creating a fat jar with the sbt-assembly plugin. I'm able to add unmanaged jars (such as the Sigar jar) by adding the following to build.sbt.
unmanagedJars in Compile +=
file("lib/hyperic-sigar-1.6.4/sigar-bin/lib/sigar.jar")
However, when I try running this, I get the following error because the *.so libraries are not included in the jar.
no libsigar-amd64-linux.so in java.library.path
org.hyperic.sigar.SigarException: no libsigar-amd64-linux.so in java.library.path
at org.hyperic.sigar.Sigar.loadLibrary(Sigar.java:172)
at org.hyperic.sigar.Sigar.<clinit>(Sigar.java:100)
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.hyperic.sigar.ptql.SigarProcessQuery.create(Ljava/lang/String;)V
at org.hyperic.sigar.ptql.SigarProcessQuery.create(Native Method)
at org.hyperic.sigar.ptql.ProcessQueryFactory.getQuery(ProcessQueryFactory.java:66)
at org.hyperic.sigar.ptql.ProcessFinder.findSingleProcess(ProcessFinder.java:44)
The libraries I want to include are in lib/hyperic-sigar-1.6.4/sigar-bin/lib/*.so and they need to be linked to a directory in the classpath within the jar. The only way I know of to do a mapping like this is:
resourceDirectory in Compile <<=
baseDirectory{ _ / "lib/hyperic-sigar-1.6.4/sigar-bin/lib" }
This causes the *.so libraries to be added to root of the jar, but not a specific directory. How can I specify a resource map to map from lib/hyperic-sigar-1.6.4/sigar-bin/lib/*.so to a directory in the classpath in my jar? What is the terminology for what I'm trying to do?
Assuming that sigar is indeed capable of loading native libs from classpath, this should do the trick:
libraryDependencies += "org.fusesource" % "sigar" % "1.6.4" classifier("native") classifier("")
Otherwise, you need to unpack them from the jar manually and provide proper java.library.path