Running a jar with the right version of scala - scala

I have a scala project that I can run with sbt. Now I want to execute it as a stand alone app. How can I run it with the scala command and ensure the right version of everything, in particular the scala-library and JVM?
PS: Just to clarify what I mean by an example from ruby.
I write some ruby script which I use routinely. For example, to apply updates a bit more cleanly then the OS does.
When I wrote the script I used ruby 1.8.3. ( Making version numbers up here as an example. ) I used the command line ruby my_script.
Now the version is ruby 2.0.1. The script will not run under this ruby because of language changes. So I use the command line ruby1.8.3 my_script.
Or rather I embed it in the shebang line.
Simalarly I want to run a jar using scala my_jar.jar but it was a built using a scala version 2.8.3 and will not run with that command. How do I make scala use the right version?

Two things you can try here:
1) The sbt-native-packager will create an executable. For example, you can create an RPM or Debian package.
2) You can create an "uber jar" using (say) sbt-assembly and package all your dependencies into a fat jar. You can specify a main function to be executed when running the jar (using the JVM java -jar a-jar-to-execute.jar command).
Both of the above are very active and widely used SBT plugins.
After your create the jar everything is in byte-code and you don't need to worry about Scala versions at that point. It will run as long as the Java (jvm) versions are compatible.

Related

scala and akka on linux: compiling and executing

I'm trying to run a scala/akka based program on a linux cluster machine. I was following the tutorial link (this was the only example I could find):
http://doc.akka.io/docs/akka/2.0/intro/getting-started-first-scala.html
It says to use the command to obtain the akka library:
git clone git://github.com/akka/akka.git
But it doesn't have any jars files inside it as per the tutorial.
I'm not able to get a basic akka-scala-sbt combo working on linux. So any help on this is much appreciated. I'm not aware (not able to find any clear source) of the commands needed to compile/execute with and without using SBT.
Java version: "1.8.0_31"
Scala version: "2.11.5"
Akka: I'm not sure, I did git clone, believe its the latest
SBT: 0.13.9
Java,Scala are already installed on the cluster, I had to just use module load.
You can start with this simple example: https://github.com/izmailoff/SmallAkkaProjectExample. It will help you to understand how to compile and run a project.
Usually the steps are:
Start SBT
Compile code
Run it from SBT
or:
Start SBT
Compile executable JAR
Run the JAR from command line
If you want a more advanced example take a look at: https://github.com/izmailoff/remote-akka-server-template
In either of these projects you don't need to download any libraries/JARs. SBT will download everything you need automatically.
In short, you need to understand how to build projects with SBT and how to run them - not related to Akka. Separately from that you need to know how Akka runs, i.e. ActorSystem, kernel, etc.

Is Scala installed multiple times if using Scala IDE, Scala on the command line, and SBT?

As far as I understand, I have multiple versions/installs of Scala to be able to access it via Eclipse, bash/OS-X shell, and for SBT:
one version of Scala as supplied with the Scala IDE;
the Scala binaries to be able to run it from within a shell; and,
Scala as part of SBT.
Is my understanding correct? If so, is there any way to run with just the one version/install for all uses?
Is my understanding correct?
No. You don't "install" Scala. You just have multiple versions of Executable Jar file of scala-compiler, scala-library etc. The version that you have on your PATH is the one that seems installed but its nothing more than running a jar file.
TO run on a specific version, just add the scala jars to the classpath of your project. If you are using SBT, you can specify the scalaVersion in your build.sbt and it will add the proper Jar to the classpath

Have jre7; JAR is jre6 compiled; can't run

I built a JAR in NetBeans, setting the Source/Binary Format to JDK6. So it's compiled to jre6. I can use the JAR (outside the IDE), but if I send it to a machine with jre7, I have to alter the environment variable to get it to execute from the JAR.
Isn't Java backwards compatible? Shouldn't this work without effort on jre7? I am not doing -anything- fancy in this program. It's a few kilobytes, couple hundred lines, import on some Map classes from java.util.
Is there a command-line option for java to use an older runtime, or an annotation to write in, to identify the class files as version 6?
Embarrassed I can't find specs to answer these questions.

Scala 2.10-M1 and 2.9 same time on Devel Machine?

Edit:
Got it sorted, SCALA_HOME + /bin to PATH sets the default interpreter; i.e. typing "scala" from bash prompt invokes 2.9.1 in my case. Just downloaded 2.10.0.M1. Invoking /path/to/2.10/bin/scala from bash brings up 2.10 REPL.
I'll just add a terminal alias for 2.10 so I don't have to type out the path manually.
Original:
Do I need a guest VM to pull this off, or in Linux, can I somehow run 2.10 and 2.9 side-by-side?
Basically, I'd like to experiment with 2.10-M1 reflection and see what runtime havoc I can wreak on case classes while continuing with general 2.9.1 development.
If not, a Kotlin-esque web demo sure would be nice to mess around with during the 2.10 evolution...
There is no problem at all with installing multiple versions of Scala; just install them in two different directories. Make sure you call the right version of scalac, scala and other executables when you want to use a specific version.
Scala does not require any system-wide settings that prevent you from having more than one version on your computer at once.
They will co-exist just fine. Remember that scala is really just a thin wrapper around Java from a runtime perspective. That is:
you can run compiled scala using the standard $JAVA_HOME/bin/java as long as scala-library.jar is on the classpath
the REPL is contained within a scala distribution

run compiled Scala Files on Java Virtual Machine

Is it possible to run scala files with Java Virtual Machine? I am trying a lot but nothing works. Can someone give me some help with command line? Thanks a lot!
Well, it depends on whether you are generating a JAR or class files, etc, but it is pretty simple: you run it like any Java program, but including the Scala library as a dependency.
java -cp .:/path/to/scala-library.jar MyApp
Scala runs on the JVM. It does not have a separate virtual machine. But it does have its own libraries, so you will need to have Scala installed wherever you're running it.
If it's compiled you will have a .class file, so you just type in
scala -cp myClassPath myPackage.myFileName
as you would with Java. You don't need the -cp option if you've navigated to your classes folder.
It is possible to run Scala classes using the java command - you can probably Google how to do it, but you would need to sort out all the correct imports and there's no reason not to just use scala as above.