scala "console" command gives error "Missing scala-library.jar" - scala

Under sbt, if I give "console" to start a REPL shell, I get the following error.
[error] stack trace is suppressed; run 'last scalaInstance' for the full output
[error] (scalaInstance) Missing scala-library.jar
[error] Total time: 1 s, completed Oct 7, 2019 12:45:07 PM
$ sbt scalaVersion
[info] Loading global plugins from C:..\plugins
[info] Loading settings for project test from plugins.sbt ...
[info] Loading project definition from C:xx\test\project
[info] Loading settings for project root from build.sbt ...
[info] scalaVersion
[info] 2.11.12
$ sbt sbtVersion
[info] sbtVersion
[info] 1.3.0

Try
assemblyOption in set: = (assemblyOption in set) .value.copy (includeScala = true)
Source: https://github.com/sbt/sbt-assembly
if this doesn't solve the issue, it could be a compatibility issue with a plugin which you might have installed, it means that you would have to delete the plugin directory in %HOME%. and %HOME%.ivy2 because it caches some artifacts locally.
If you are not able to find the plugin try deleting %HOME%.ivy2 and running the console again, as it will remove the cached artifacts.
You might want to refer
https://pt.coredump.biz/questions/50313823/why-does-running-tests-through-jenkins-user-on-build-slave-fail-with-missing-scalalibraryjar
I hope it helps.

Somewhat unrelated but I ran into this error on one of my projects and deleting all caches (.m2, .ivy2, coursier) was not enough to fix this. I also had to clear out my .sbt folder (I assume emptying the boot directory). Be sure to backup any settings you still might need.

can you try updating scala version in your sbt build?
for example,
scalaVersion := "2.13.0"

Related

Using sbt run on simple tutorial example does not execute

On a Windows machine, I am running through the getting started tutorial here: https://www.scala-sbt.org/1.x/docs/sbt-by-example.html
I am able to start the sbt shell and compile but using the run command just hangs and I am forced to Ctrl-C to exit sbt.
Running scala against the JAR file directly also works as expected.
Sample output is the following:
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading project definition from S:\foo-build\project
[info] Loading settings for project foo-build from build.sbt ...
[info] Set current project to foo-build (in build file:/S:/foo-build/)
[info] sbt server started at local:sbt-server-8c6933abcb6848dbd242
sbt:foo-build> about
[info] This is sbt 1.2.8
[info] The current project is ProjectRef(uri("file:/S:/foo-build/"), "foo-build") 0.1.0-SNAPSHOT
[info] The current project is built against Scala 2.12.7
[info] Available Plugins
[info] - sbt.ScriptedPlugin
[info] - sbt.plugins.CorePlugin
[info] - sbt.plugins.Giter8TemplatePlugin
[info] - sbt.plugins.IvyPlugin
[info] - sbt.plugins.JUnitXmlReportPlugin
[info] - sbt.plugins.JvmPlugin
[info] - sbt.plugins.SbtPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.12.7
sbt:foo-build> compile
[success] Total time: 0 s, completed Feb 7, 2019 4:40:45 PM
sbt:foo-build> run
Terminate batch job (Y/N)? Y
This is enough of an answer for my own purposes.
I was able work around this issue by adding a line to my build.sbt file. After adding ThisBuild / scalaVersion := "2.12.8" I was able to use the run command. Following the tutorial linked in the main question I would not expect to have to do this but perhaps someone could shed some light.

Scala: Sbt can run project, but calling the jar results in ClassNotFoundError

I have a project that will compile/package/run with SBT, but when I call the jar from the command line with Scala, I get a 'java.lang.ClassNotFoundException'.
build.sbt:
name := "coloring"
version := "0.1"
scalaVersion := "2.12.5"
libraryDependencies ++= Seq("org.scala-graph" %% "graph-core" % "1.12.5")
running sbt run results in:
[info] Compiling 1 Scala source to /path/to/dir/coloring/target/scala-2.12/classes ...
[warn] there was one feature warning; re-run with -feature for details
[warn] one warning found
[info] Done compiling.
[info] Packaging /path/to/dir/coloring/target/scala-2.12/coloring_2.12-0.1.jar ...
[info] Done packaging.
[info] Running Main
[success] Total time: 6 s, completed May 21, 2018 12:55:19 PM
However running scala target/scala-2.12/coloring_2.12-0.1.jar results in
java.lang.ClassNotFoundException: scalax.collection.GraphBase$InnerNode
What could be going wrong here? How is it possible that sbt run works, but when you call the jar directly, it fails?
EDIT: I solved the problem using sbt-assembly, but (1) I don't think this should be necessary, and (2) in similar projects in the past, I have used the exact same build.sbt and library/imports/etc and the project works when called from the command line.
If you don't want to assemble an uber-jar or similar, you'll need to have the dependencies on the classpath.
From the command line, that looks like:
scala -classpath my_1st.jar:my_2nd.jar:my_3rd.jar <whatever command>
So in your case, I imagine that looks like:
scala -classpath path/to/scala-graph.jar target/scala-2.12/coloring_2.12-0.1.jar
You can shorten -classpath to -cp
Here are quite a few other options: Include jar file in Scala interpreter
Libraries that you use need to be in the Java classpath. If you don't want to use assembly to include dependent libraries into a fat jar, you need to add them to the classpath some other way. For example via command line:
java -cp yourApp.jar:somelib.jar:someotherlibjar your.main.Class
java -cp yourApp.jar:libs/* your.main.Class

Exclude test dependency resolution from `sbt compile`

When running sbt compile, dependencies marked as test are still resolved even though they are not later included later in compilation. It seems like this should only happen during the test:compile task. Is there a way to exclude test dependencies from being resolved and downloaded during the compile task?
Here is an example with the org.mockito#mockito-all dependency. I have it declared as test-only:
"org.mockito" % "mockito-all" % "1.9.0" % "test"
However, when (clearing it from my local Ivy cache and) running sbt compile, it gets needlessly downloaded:
$ sbt compile
[info] Loading global plugins from /Users/rbrainard/.sbt/plugins
[info] Loading project definition from /Users/rbrainard/Development/spitball/project
[info] Set current project to spitball (in build file:/Users/rbrainard/Development/spitball/)
[info] Updating {file:/Users/rbrainard/Development/spitball/}spitball...
[info] Resolving org.mockito#mockito-all;1.9.0 ...
[info] downloading http://repo1.maven.org/maven2/org/mockito/mockito-all/1.9.0/mockito-all-1.9.0.jar ...
[info] [SUCCESSFUL ] org.mockito#mockito-all;1.9.0!mockito-all.jar (2075ms)
[info] Done updating.
[success] Total time: 7 s, completed May 28, 2014 4:51:20 PM
In sbt the update task resolves all dependencies for all configurations. Sbt makes use of Ivy, which promotes a very interesting "configuration" aspect to be able to separately resolve disparate classpaths at the same time.
Sbt is not only resolving your test classpath, but also runtime, the scala-tools (compiler, scaladoc, repl) and more.
Please read: https://ant.apache.org/ivy/history/latest-milestone/terminology.html for more information on the design of Ivy, which is why sbt attempts to do all resolution at once for all configurations.

How to find out which dependency caused a particular library to be downloaded?

When runing my SBT project, there is a line in console output:
[info] downloading http://repository/nexus/content/groups/public/org/jboss/netty/netty/3.2.3.Final/netty-3.2.3.Final.jar ...
[info] [SUCCESSFUL ] org.jboss.netty#netty;3.2.3.Final!netty.jar(bundle) (651ms)
How to find out which project dependency caused the netty.jar to be downloaded?
This plugin should be able to help: https://github.com/jrudolph/sbt-dependency-graph/
Another way would be to turn on full debug in your build.sbt as follows:
ivyLoggingLevel := UpdateLogging.Full
logLevel := Level.Debug
and then you could parse the output of sbt update
For example, If I wanted to know where logback-core comes from in my sample project, I could run
sbt update | grep logback-core
And I would get multiple such lines, telling me that it comes with logback-classic:
[debug] == resolving dependencies ch.qos.logback#logback-classic;1.0.10->ch.qos.logback#logback-core;1.0.10 [compile->master(*)]

how to set main class in SBT 0.13 project

Could you guys please explain to me how to set main class in SBT project ? I'm trying to use version 0.13.
My directory structure is very simple (unlike SBT's documentation). In the root folder I have build.sbt with following content
name := "sbt_test"
version := "1.0"
scalaVersion := "2.10.1-local"
autoScalaLibrary := false
scalaHome := Some(file("/Program Files (x86)/scala/"))
mainClass := Some("Hi")
libraryDependencies ++= Seq(
"org.scalatest" % "scalatest_2.10" % "2.0.M5b" % "test"
)
EclipseKeys.withSource := true
And I have subfolder project with single file Hi.scala which contains following code
object Hi {
def main(args: Array[String]) = println("Hi!")
}
I'm able to compile it by calling sbt compile but sbt run returns
The system cannot find the file C:\work\externals\sbt\bin\sbtconfig.txt.
[info] Loading project definition from C:\work\test_projects\sbt_test\project
[info] Set current project to sbt_test (in build file:/C:/work/test_projects/sbt_test/)
java.lang.RuntimeException: No main class detected.
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed Apr 8, 2013 6:14:41 PM
You need to put your application's source in src/main/scala/, project/ is for build definition code.
Try to use an object and extend it from App instead of using class
object Main extends App {
println("Hello from main scala object")
}
Here is how to specify main class
mainClass in (Compile,run) := Some("my.fully.qualified.MainClassName")
For custom modules in SBT (0.13), just enter on the SBT console:
project moduleX
[info] Set current project to moduleX (in build file:/path/to/Projects/)
> run
[info] Running main
to switch scope to moduleX, as define in Built.scala. All main classes within that scope will be detected automatically. Otherwise you get the same error of no main class detected.
For God's sake, SBT does not tell you that the default scope is not set. It has nothing to do with default versus non default source folders but only with SBT not telling anything that it doesn't know which module to use by default.
Big Hint to typesafe: PLEASE add a default output like:
[info] Project module is not set. Please use ''project moduleX'' set scope
or set in Built file (LinkToDocu)
at the end of SBT start to lower the level of frustration while using SBT on multi module projects.....
I had the same issue: was mode following the tutorial at http://www.scala-sbt.org/0.13/docs/Hello.html, and in my opinion, as a build tool sbt's interaction and error messages can be quite misleading to a newcomer.
It turned out, hours of head scratching later, that I missed the critical cd hello line in the example each time. :-(
If you have multiple main methods in your project you can add the following line to your build.sbt file:
val projectMainClass = "com.saeed.ApplicationMain"
mainClass in (Compile, run) := Some(projectMainClass)
If you want to specify the class that will be added to the manifest when your application is packaged as a JAR file, add this line to your build.sbt file:
mainClass in (Compile, packageBin) := Some(projectMainClass)
You can also specify main class using run-main command in sbt and activator to run:
sbt "run-main com.saeed.ApplicationMain"
or
activator "run-main com.saeed.ApplicationMain"
There are 4 options
you have 1 main class
sbt run and sbt will find main for you
you have 2 or more main classes
sbt run and sbt will propose to select which one you want to run.
Multiple main classes detected, select one to run:
[1] a.b.DummyMain1
[2] a.b.DummyMain2
Enter number:
You want to set main class manually.
mainClass in run := Some("a.b.DummyMain1")
You could run with main class as parameter
sbt runMain a.b.DummyMain1
If the Main class is in a different project then by setting the below command in build.sbt would work:
addCommandAlias("run", "; project_folder/run")
I had the same issue. Resolved it after adding PlayMinimalJava plugin in build.sbt.
Not sure how it got fixed, if someone can highlight how PlayMinimalJava solves it, would be great.
enablePlugins(PlayMinimalJava)
My build.sbt looks like this
organization := "org"
version := "1.0-SNAPSHOT"
scalaVersion := "2.13.1"
libraryDependencies += guice
enablePlugins(PlayMinimalJava)
Log
C:\Users\KulwantSingh\repository\pdfdemo>sbt run
Java HotSpot(TM) Client VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading settings for project pdfdemo-build from plugins.sbt ...
[info] Loading project definition from C:\Users\KulwantSingh\repository\pdfdemo\project
[info] Loading settings for project pdfdemo from build.sbt ...
[info] Set current project to pdfdemo (in build file:/C:/Users/KulwantSingh/repository/pdfdemo/)
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
--- (Running the application, auto-reloading is enabled) ---
[info] p.c.s.AkkaHttpServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
(Server started, use Enter to stop and go back to the console...)
[info] Compiling 6 Scala sources and 2 Java sources to C:\Users\KulwantSingh\repository\pdfdemo\target\scala-2.13\classes ...
[info] p.a.h.EnabledFilters - Enabled Filters (see <https://www.playframework.com/documentation/latest/Filters>):
play.filters.csrf.CSRFFilter
play.filters.headers.SecurityHeadersFilter
play.filters.hosts.AllowedHostsFilter
[info] play.api.Play - Application started (Dev) (no global state)
Incase if someone made a silly mistake like I did.
Check if your project is sbt-multi project.
If it is make sure to provide sbt moduleName/run instead sbt run
I keep making this mistaken when I switch between multi and single projects.