How do I compile Java 8 sources with SBT - sbt

I am trying to compile a project containing some Java 8 source files using lambdas using SBT 0.13.7.
Now, I set
-java-home /Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Home
in
/usr/local/etc/sbtopts
And apparently SBT does take this into consideration, because if I supply the wrong path it complains about no java being present.
However, when I try compiling, I get the following
[error] /Users/edafinov/GitRepos/Java8SBTTest/src/main/java/Main.java:10: error: illegal start of expression
[error] new ArrayList<Integer>().stream().filter(i -> i%2 ==0).count();
[error] ^
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code
[error] Total time: 1 s, completed Dec 26, 2014 12:28:55 PM
This seems to indicate that SBT does not use javac 1.8, but rather the 1.7 one which is the default one for my system.
What am I doing wrong?
Thank you in advance

Apparently if you have a JDK in the path, SBT would try to use that one, even when you have explicitly set the -java-home option. I solved my problem by removing the default JDK from $PATH

Related

Translating Lightbend activator to sbt

I'm trying to work through the book Reactive Web Applications, but it seems that it's already a bit out of date in that lightbend activator has been EOL'd so I can't download it (or at least can't figure out how to and probably shouldn't even if I could).
I'm faced with the problem of creating the new project, which in the book would be done via the command
activator new twitter-stream play-scala-2.4
I tried what I though was the equivalent sbt command but got the following error.
urban:playground dhosek$ sbt new playframework/play-scala-seed.g8
[info] Set current project to playground (in build
file:/Users/dhosek/playground/)
[error] Not a valid command: new (similar: set)
[error] Not a valid project ID: new
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: new (similar: name, run, runner)
[error] new
[error] ^
Any help?
It appears that the problem is that brew installs an old version of sbt.

Error running Kafka on Cloudera quickstart: assembly-package-dependency not valid

I have downloaded Kafka from apache and extracted it to its own folder. Following the quickstart, I also installed sbt, but at the third line in the sbt commands (I am launching the terminal from INSIDE the kafka folder, I get:
[error] Not a valid command: assembly-package-dependency
[error] Not a valid project ID: assembly-package-dependency
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: assembly-package-dependency (similar: sbt-dependency)
[error] assembly-package-dependency
[error] ^
I have been looking for all day for an answer, but found none which would start my server. The exception when I try the kafka-server-start.sh is always
unable to find main class Kafka.kafka
I also tried "gradle" the first time, but the problem was the same. I have no chance of upgrading to Cloudera-Express to use the parcel installer: my PC is not good enough to support it.
I am quite desperate: please help me!
I found that sbt update etc didn't quite do the job, so in the end I found another answer suggesting:
In the kafka folder:
./gradlew jar

sbt-proguard issue with Java 1.8

I'm trying to get smaller scalar executable jar file with sbt-proguard.
I added project/plugin.sbt these two lines of code:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.13.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-proguard" % "0.2.2")
The first one is to get uberjar file, and I could get uberjar with sbt assembly that works fine.
Then, I executed sbt proguard:proguard to get this error message.
[error] Error: Can't read [/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/jre/lib/rt.jar] (Can't process class [apple/applescript/AppleScriptEngine.class] (Unsupported class version number [52.0] (maximum 51.0, Java 1.7)))
java.lang.RuntimeException: Proguard failed with exit code [1]
at scala.sys.package$.error(package.scala:27)
...
at java.lang.Thread.run(Thread.java:745)
[error] (proguard:proguard) Proguard failed with exit code [1]
From the hint from this post: ProGuard says Unsupported class version number [52.0] (maximum 51.0, Java 1.7) with sbt-proguard,
I switched to both Java 1.7 and Java 1.6 with export JAVA_HOME=/usr/libexec/java_home -v '1.6*' command to run proguard to get the slim-lined jar file, but this doesn't run.
Invalid or corrupt jarfile target/scala-2.11/proguard/myproject_2.11-1.0.jar
What might be wrong? These are the lines that are added to build.sbt.
proguardSettings
ProguardKeys.options in Proguard ++= Seq("-dontnote", "-dontwarn", "-ignorewarnings")
ProguardKeys.options in Proguard += ProguardOptions.keepMain("core.HelloWorld")
I believe this is documented in the pro guard docs.
Running your application with java -classpath <jarpath> --class classname <program-arguments> should work.
This happens because pro guard by default removes all MANIFEST files from the jar hence the java runtime cannot find the jar class entries. Another way to do this would be to keep the MANIFEST.md file and run it using the java -jar option but I have never tried that.
Define a recent Proguard version that supports Java 1.8
ProguardKeys.proguardVersion in Proguard := "5.3.3"
Also a couple of useful ones if you run out of mem are
javaOptions in (Proguard, ProguardKeys.proguard) := Seq("-Xmx2G")
javaOptions in (Proguard, ProguardKeys.proguard) += "-Xss1G"

Terminating SBT build build elegantly with simple error message

I have an SBT build for a project that requires Java 8 and I have also included some Java 8 features (mainly java.time package elements) in the build itself. If I attempt to build the project on an earlier JVM (such as 1.7), I get a whole load of errors and exceptions when I run the sbt command.
So, I decided to add an initialize property to detect the JVM in use and terminate the build with an elegant message if the JVM in use wasn't at least version 8. Of course, this only works if I use it to build the builder (if I put it in ./build.sbt, then I still get errors if using JVM 1.7 or earlier because the java.time elements are not present). So I have a ./project/build.sbt file that contains the following:
initialize := {
val _ = initialize.value
if (!scala.util.Properties.isJavaAtLeast ("1.8")) {
sys.error ("My project requires Java 8 or later")
}
}
This works, but rather inelegantly. If I run sbt with Java 7, then I get the following output from the build:
java.lang.RuntimeException: My project requires Java 8 or later
at scala.sys.package$.error(package.scala:27)
at $ed9be51e9ab4a59b3b3b$$anonfun$$sbtdef$1.apply(C:\Users\me\Documents\src\myproject\project\build.sbt:55)
at $ed9be51e9ab4a59b3b3b$$anonfun$$sbtdef$1.apply(C:\Users\me\Documents\src\myproject\project\build.sbt:47)
at scala.Function1$$anonfun$compose$1.apply(Function1.scala:47)
at sbt.EvaluateSettings$MixedNode.evaluate0(INode.scala:177)
at sbt.EvaluateSettings$INode.evaluate(INode.scala:135)
at sbt.EvaluateSettings$$anonfun$sbt$EvaluateSettings$$submitEvaluate$1.apply$mcV$sp(INode.scala:67)
at sbt.EvaluateSettings.sbt$EvaluateSettings$$run0(INode.scala:76)
at sbt.EvaluateSettings$$anon$3.run(INode.scala:72)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
[error] My project requires Java 8 or later
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
If I place the same code in an empty ./build.sbt file, and run using Java 7 then I simply get:
[error] My project requires Java 8 or later
[error] Use 'last' for the full log.
Project loading failed: (r)etry, (q)uit, (l)ast, or (i)gnore?
That is, I don't get the exception message and stack trace.
Is there a way to make the build build exit more elegantly, so that I get output of the latter form rather than the former?

Different errors during 'sbt' and 'sbt run' in scalafx-ensemble

I downloaded the scalafx-ensemble project. When I run sbt in the project's folder I'm facing the following error:
[info] Set current project to scalafxEnsemble (in build file:/C:/dev/sample/scalafx-ensemble-master/)
[ERROR] Terminal initialization failed; falling back to unsupported
java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
at jline.TerminalFactory.create(TerminalFactory.java:101)
at jline.TerminalFactory.get(TerminalFactory.java:159)
at sbt.JLine$.sbt$JLine$$terminal(LineReader.scala:87)
at sbt.JLine$.withTerminal(LineReader.scala:91)
at sbt.JLine$.usingTerminal(LineReader.scala:97)
at sbt.JLine$.createReader(LineReader.scala:103)
at sbt.FullReader.<init>(LineReader.scala:135)
at sbt.BasicCommands$$anonfun$shell$1.apply(BasicCommands.scala:149)
...
java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
at sbt.JLine$$anonfun$usingTerminal$1.apply(LineReader.scala:98)
at sbt.JLine$$anonfun$usingTerminal$1.apply(LineReader.scala:97)
at sbt.JLine$.withTerminal(LineReader.scala:92)
at sbt.JLine$.usingTerminal(LineReader.scala:97)
at sbt.JLine$.createReader(LineReader.scala:103)
at sbt.FullReader.<init>(LineReader.scala:135)
at sbt.BasicCommands$$anonfun$shell$1.apply(BasicCommands.scala:149)
at sbt.BasicCommands$$anonfun$shell$1.apply(BasicCommands.scala:146)
at sbt.Command$$anonfun$command$1$$anonfun$apply$1.apply(Command.scala:31)
at sbt.Command$$anonfun$command$1$$anonfun$apply$1.apply(Command.scala:31)
at sbt.Command$.process(Command.scala:95)
at sbt.MainLoop$$anonfun$1$$anonfun$apply$1.apply(MainLoop.scala:100)
...
[error] java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
[error] Use 'last' for the full log.
If I run sbt run I'm getting the following:
at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3193)
at scala.tools.nsc.typechecker.Typers$Typer.handleOverloaded$1(Typers.scala:3190)
at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3193)
at scala.tools.nsc.typechecker.Typers$Typer.handleOverloaded$1(Typers.scala:3190)
[error] (compile:compile) java.lang.StackOverflowError
[error] Total time: 9 s, completed Jan 6, 2014 4:47:49 PM
What am I doing wrong?
This is likely problem with outdated SBT launcher. Make sure that your SBT is v.0.13 or newer.
I fixed the path to jfxrt.jar in build.sbt from
System.getenv("JAVA_HOME") + "jre/lib/jfxrt.jar"
to
System.getenv("JAVA_HOME") + "/lib/jfxrt.jar"
which is correct for my system. With the change, sbt run works fine.

Resources