Which version of Java does SBT use? - scala

How to find out, which version of JDK SBT uses?
My laptop has both JDK 1.6 and JDK 1.7 installed, so I was just wondering.

You can use eval at the sbt prompt with the appropriate system properties:
> eval System.getProperty("java.version")
[info] ans: String = 1.7.0_45
> eval System.getProperty("java.home")
[info] ans: String = /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
(The other answers are fine too, this is just another method.)

Just run sbt console and you'll get something like this:
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
Which shows you the version of Java being used.
Cheers
JC

It's actually easy.
Find the command line of SBT (cat `which sbt`) and see which Java it is.
For example, my SBT command line begins with:
/usr/bin/java -Xmx512M
And /usr/bin/java -version tells the Java version, in my case it is 1.7.

sbt -v is another way:
# sbt -v
[process_args] java_version = '1.7.0_121'
# Executing command line:
java
-Xms1024m
-Xmx1024m
-XX:ReservedCodeCacheSize=128m
-XX:MaxPermSize=256m
-jar
/usr/share/sbt-launcher-packaging/bin/sbt-launch.jar
[info] Loading project definition from /root/.sbt/0.13/staging/a6d8e5030b69785a9763/build/project
[info] Set current project to xx (in build file:/build/)
>

Here's a batch file method I created to get that info immediately from the command line (Windows only, but you can do something similar in a shell script):
#echo off
setlocal
sbt "eval System.getProperty(\"java.version\")" "eval System.getProperty(\"java.home\")"
#echo on

Related

How to pass system properites in comand line to scala 3 executable

With scala 2 it was possible to pass system properties in the command line using -D<propname>=<propvalue> like in the following example:
$scala -Dpath.to.folder=/opt/myfolder
Scala v3 does not accept -D in the command line anymore:
$ scala -Dpath.to.folder=/opt/myfolder
bad option '-Dpath.to.folder=/opt/myfolder' was ignored
Welcome to Scala 3.1.0 (11.0.9.1, Java OpenJDK 64-Bit Server VM).
One possible solution (workaround) is to pass the properties through the environment:
$ env SYS_PROPS="-Dpath.to.folder=/opt/myfolder" scala
Welcome to Scala 3.1.0 (11.0.9.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> println(System.getenv().get("SYS_PROPS"))
-Dpath.to.folder=/opt/myfolder
scala>
How is it possible to pass system properties in the command line of scala 3 executables?
scala --help does not provide any useful info. Actually in scala 3.1.0 scala --help and scalac --help print the same message in the stdout.

Template not found for: -Dsbt.version 0.13.13 lagom/lagom-scala.g8

Good afternoon,
I am trying to follow this hello world tutorial of lagom, however when running the command:
sbt new -Dsbt.version=0.13.13 lagom/lagom-scala.g8
I get the following error:
Template not found for: -Dsbt.version 0.13.13 lagom/lagom-scala.g8
As stated in the setup part of the tutorial I have checked my java version:
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
My javac version:
javac 1.8.0_131
And my sbt version:
[info] 0.13.15
I have trying running the command with my sbt version as the parameter:
sbt new -Dsbt.version=0.13.13 lagom/lagom-scala.g8
But it still throws the no template error, could you please point out what am I doing wrong?
Which comply with the prerequisites for the tutorial
Turns out I had to remove the -Dsbt.version=0.13.13 parameter of the commmand.
Running it as:
sbt new lagom/lagom-scala.g8
Removes the error and it works correctly.
The problem was not the extra flag. The documentation is correct.
You have to use it before the new command
sbt -Dsbt.version=0.13.13 new lagom/lagom-scala.g8

scala: Error occurred during initialization of VM on Ubuntu 12.04

I tried to work with easy example of scala language.
I installed sbt successfully. When run sbt -h I see help message from sbt:
Usage: sbt [options]
-h | -help print this message
-v | -verbose this runner is chattier
-d | -debug set sbt log level to debug
-no-colors disable ANSI color codes
-sbt-create start sbt even if current directory contains no sbt project
-sbt-dir <path> path to global settings/plugins directory (default: ~/.sbt)
-sbt-boot <path> path to shared boot directory (default: ~/.sbt/boot in 0.11 series)
-ivy <path> path to local Ivy repository (default: ~/.ivy2)
-mem <integer> set memory options (default: 1536, which is -Xms1536m -Xmx1536m -XX:MaxPermSize=384m -XX:ReservedCodeCacheSize=192m) ....
but when I tried to run in the directory with scala assignments (it's Coursera course) I caught error:
nazar_art#nazar-desctop:~/scala/example$ sbt
Detected sbt version 0.12.1
Starting sbt: invoke with -help for other options
Error occurred during initialization of VM
Could not reserve enough space for object heap
but it should open the sbt command prompt. Smt like this:
shell$ cd /path/to/progfun-project-directory # This is the shell of the operating system
shell$ sbt
> _ # This is the sbt shell
UPDATE:
After I add this following line
java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jardirname $0/sbt-launch.jar "$#"
at /usr/bin I see next output:
nazar_art#nazar-desctop:/usr/bin$ sbt
bash: /usr/bin/sbt: Permission denied
nazar_art#nazar-desctop:/usr/bin$ sudo sbt
sudo: sbt: command not found
- Why this happen?
- How to solve this issue?
First of all, The example folder name (and the folder name that you have installed sbt) cannot include "!". this probably applies to other special characters as well. Even, I recommend to install SBT in a folder that has no space on its path (and the same thing for examples folder).
For example, if you have installed SBT (or examples folder) on a path like:
/Users/dashti/My Programs/sbt-0.12.1
You can re-install it a path like:
/Users/dashti/MyPrograms/sbt-0.12.1
By applying this naming change, if the error is gone, please ignore the rest of this post.
Otherwise, to fix the mentioned error, first find sbt executable script, then edit it and correct any problem in it:
If you are using a UNIX-based operating system, please follow these steps:
1- Find SBT: Open a terminal and type this command:
which sbt
and you will receive a path like:
/Users/dashti/Documents/scala-2.10.0/bin/sbt
and in this file, there is a command for executing a java program, like:
java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$#"
2- Edit sbt script: by entering this command in terminal:
vim /Users/dashti/Documents/scala-2.10.0/bin/sbt
you will be able to edit this file and maybe you should change the memory usage arguments to a lower specification ( You should think about whether you have enough RAM to be running the sbt). For example, this one will be a better choice:
java -Xms64M -Xmx256M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=64M -jar `dirname $0`/sbt-launch.jar "$#"
SBT needs more memory allocated to the JVM.
This can be done in build.sbt:
javaOptions ++= Seq(
"-Xms64m",
"-Xmx256M",
"-XX:+CMSClassUnloadingEnabled",
"-XX:MaxPermSize=64M"
);
Creating an sbt file(no extentions) and running ./sbt is better, but with the code below(#MohammadDashti's suggestion), sbt-launch.jar needs to be in the project folder.
java -Xms512M -Xmx1024M -Xss1M -XX:+CMSClassUnloadingEnabled \
-XX:MaxPermSize=128M -jar `dirname $0`/sbt-launch.jar "$#"
Later you could add javaagent:/path/to/jrebel -Drebel.remoting_plugin=true and re-deploy changes on save with a free JRebel for Scala license.

Setting up sbt to use Java 7 for compilation?

I'm getting compile errors when running the compile task as the sources reference new classes in java.nio.file package that only appeared in Java 7.
I have the following in build.sbt:
javaHome := Some(file("/opt/jdk/jdk1.7.0"))
fork := true
In sbt:
> show java-home
[info] Some(/opt/jdk/jdk1.7.0)
It compiles and runs fine in Eclipse. How can I set up sbt to use Java 7 for compilation?
The most reliable (perhaps only) way to do this at the moment it to start SBT with java in the JDK7 folder.
Modify your sbt launcher script; or use this one that allows you to specify Java Home (and so much more!) as command line options.
~/code/scratch/20111009 sbt -java-home /Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home
Starting sbt: invoke with -help for other options
[info] Loading global plugins from /Users/jason/.sbt/plugins
[info] Set current project to default-3e990a (in build file:/Users/jason/code/scratch/20111009/)
> console
[info] Compiling 1 Scala source to /Users/jason/code/scratch/20111009/target/scala-2.9.1/classes...
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.9.1.final (OpenJDK 64-Bit Server VM, Java 1.7.0-internal).
Type in expressions to have them evaluated.
Type :help for more information.
scala> java.util.Objects.equals(null, null)
res0: Boolean = true
Simply setting javaHome := Some(file("/Library/Java/JavaVirtualMachines/openjdk-1.7-x86_64/Contents/Home")) changes the Java version used to compile and fork processes, but does not change the version of the Java standard library on the classpath, nor the version used to run tests, which are always run the the same JVM as SBT.
If you use Linux or Mac, another possibility is to look at jenv, a command line Java manager.
It allows you to choose per project which JDK to use.
I use virtualenv, which is a tool from the Python ecosystem. In a nutshell, it is a shell script which allows you to change your PATH variable easily and get back to what it was before, if you need to.
First install virtualenvwrapper (a wrapper around virtualenv):
$ apt-get install virtualenvwrapper
Now create a virtual environment for, say, Java8 with Scala-2.11.
$ mkvirtualenv j8s11
Now, adjust ~/.virtualenvs/j8s11/bin/postactivate so that you define locations for all your tools. You can see an example below which works for me:
#!/bin/bash
JAVA_VERSION=1.8.0_31
SCALA_VERSION=2.11.5
SBT_VERSION=0.13.7
ANT_VERSION=1.9.4
M2_VERSION=3.2.5
GRADLE_VERSION=1.6
PLAY_VERSION=2.3.7
ACTIVATOR_VERSION=1.2.12
IDEA_VERSION=IC-135.475
PYCHARM_VERSION=community-3.4.1
TOOLS_HOME=/opt/developer
export JAVA_HOME=${TOOLS_HOME}/jdk${JAVA_VERSION}
export SCALA_HOME=${TOOLS_HOME}/scala-${SCALA_VERSION}
export SBT_HOME=${TOOLS_HOME}/sbt-${SBT_VERSION}
export ANT_HOME=${TOOLS_HOME}/apache-ant-${ANT_VERSION}
export M2_HOME=${TOOLS_HOME}/apache-maven-${M2_VERSION}
export GRADLE_HOME=${TOOLS_HOME}/gradle-${GRADLE_VERSION}
export PLAY_HOME=${TOOLS_HOME}/play-${PLAY_VERSION}
export ACTIVATOR_HOME=${TOOLS_HOME}/activator-${ACTIVATOR_VERSION}
export IDEA_HOME=${TOOLS_HOME}/idea-${IDEA_VERSION}
export PYCHARM_HOME=${TOOLS_HOME}/pycharm-${PYCHARM_VERSION}
PATH=${PYCHARM_HOME}/bin:$PATH
PATH=${IDEA_HOME}/bin:$PATH
PATH=${ACTIVATOR_HOME}:$PATH
PATH=${PLAY_HOME}:$PATH
PATH=${GRADLE_HOME}/bin:$PATH
PATH=${M2_HOME}/bin:$PATH
PATH=${ANT_HOME}/bin:$PATH
PATH=${SBT_HOME}/bin:$PATH
PATH=${SCALA_HOME}/bin:$PATH
PATH=${JAVA_HOME}/bin:$PATH
export PATH
Now you can just use workon to switch between environments. Example:
rgomes#terra:~$ workon j8s11
(j8s11)rgomes#terra:~$ java -version
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
(j8s11)rgomes#terra:~$ scala -version
Scala code runner version 2.11.5 -- Copyright 2002-2013, LAMP/EPFL
(j8s11)rgomes#terra:~$ workon j7s10
(j7s10)rgomes#terra:~$ java -version
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
(j7s10)rgomes#terra:~$ scala -version
Scala code runner version 2.10.4 -- Copyright 2002-2013, LAMP/EPFL
I'm assuming you want to change whatever you have set in JAVA_HOME by default, which you can do when invoking sbt:
JAVA_HOME=<path-to-jdk-home> sbt
This works for me on OSX with sbt 0.13.8
change javacOption to 1.7? I don't think setting the javaHome is necessary.

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter?
My code is working when I compile from scalac:
scalac script.scala -classpath *.jar
But I would like to be able to include a jar file when running the interpreter.
In scala2.8,you can use
scala>:jar JarName.jar
to add a jar to the classpath.
In Scala 2.8.1, it is not :jar but :cp
And in Scala 2.11.7 it is not :cp but :re(quire)
According to scala executable help all options of scalac are allowed ,
so you can run scala -classpath some.jar, i've just tried and it looks like it works
Include multiple jars int Scala REPL 2.10.0-RC2
scala -classpath my_1st.jar:my_2nd.jar:my_3rd.jar
in my case i am using Scala code runner version 2.9.2. and i had to add quotation marks.
I am using this jar files:
jdom-b10.jar, rome-0.9.jar
and everything goes fine with this:
scala -classpath "*.jar" feedparser.scala
In Scala version 2.11.6 from scala REPL use :require, can best be figured out by using :help from REPL
For example:
$ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_45).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :require lift-json_2.11-3.0-M5-1.jar
Added '<path to lift json library>/lift-json/lift-json_2.11-3.0-M5-1.jar' to classpath.
Scala version 2.11.5:
Here is an example of adding all jars in your ivy cache:
scala -cp /Users/dbysani/.ivy2/cache/org.apache.spark/spark-streaming_2.10/jars/*
scala> import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.StreamingContext
You can also create a local folder of all the jars that you need to get added and add it in a similar way.
Hope this helps.
"lib/*.jar" generates a list with blank between items not ":" or ";" as required.
Since Java 6 "lib/*" should work, but sometimes doesn't (classpath is set somewhere else)
I use a script like:
Windows:
#rem all *.jars in lib subdirectory
#echo off
set clp=.
for %%c in (lib\*.jar) do call :Setclasspath %%c
echo The classpath is %clp%
scala -classpath %clp% script.scala
exit /B %ERRORLEVEL%
:Setclasspath
set clp=%clp%;%~1
exit /B 0
Linux:
#!/bin/bash
#all *.jars in lib subdirectory
clp="."
for file in lib/*
do
clp="$clp:$file"
done
echo $clp
scala -classpath $clp script.scala