Compiling a scala class and make it available in Scala Context - scala

I have a class written in Scala and I am trying to make it available to the Scala Context so that I can make use of it for further processing. The problem is that I need to run this from the shell and I am having a hard time figuring out how to compile the class and make it available to the context.
I am aware of compiling the class and making use of directly, but I am not able to figure out how to do the same on the Scala shell. Any pointers in this regard would be great.

In the Scala REPL you can use the command :cp <path> to add a directory or JAR (that contains your compiled Scala class) to the classpath, so that it is available for the REPL to use.
(Ofcourse, replace <path> in that command with the actual directory or JAR path).
To see what other commands are available in the Scala REPL, use the command :help.

Related

Possible to compile and run scala-akka scripts from command-line without build tools like sbt?

I am a beginner learning to program in scala-akka and I have had no problems running my scripts on IntelliJ IDE / and 'sbt run'. However, I can't seem to find any resources that teaches me how to manually use scalac and the akka jar dependency to compile and run just from the command-line. Can anyone point me in the right direction?
Let's assume you have Scala and Akka installed somewhere under /home/leo/apps/ and Scala binaries are searchable (e.g. export PATH=$PATH:home/leo/apps/scala-2.11.8/bin)
Next, let's say you have a Scala main app Tweets.scala along with a few supplementary classes packaged in akkastreams under /home/leo/myproject/:
akkastreams/
Tweets.scala
Author.scala
HashTag.scala
Message.scala
...
Here's how you'll compile and run the app:
cd /home/leo/myproject/
# Compile all files in package akkastreams:
scalac -cp "/home/leo/apps/akka-2.4.9/lib/akka/*" akkastreams/*.scala
# Run the main app Tweets (object Tweets extends App):
# Note that classpath includes also current subdir '.'
scala -cp "/home/leo/apps/akka-2.4.9/lib/akka/*:." akkastreams.Tweets
A few notes:
You could include only specific Akka jars instead of all of them.
Without dependencies and versioning being managed by sbt, you'll need to manually maintain version consistency between Scala's bundled Akka libraries versus Akka's own ones.
While it's a good exercise to see how things are done in a crude way, it's obviously unproductive to do this on a regular basis.
In my opinion You should perform scalac and scala with classpath parameter and selected library jar file.
By the way it's still more convenient to use sbt.

How can I compile and load a new source file in Scala from the REPL?

In the Scala shell, I would like to be able to load a Scala source file into the current session. The catch is that I would like that file to be arbitrary Scala source code – including, in particular, things such as package declarations.
Is this possible? If so, how do I do it?
Yes, it is possible. Pass the scala source file ("test.scala" in the snippet below) as an argument to the scala command:
scala test.scala
This will compile and execute the scala source in the file.

Unable to run a HelloWorld Scala program on Ubuntu

I have my very first Scala program, which is as simple as:
object HelloWorld{
def main(args: Array[String]){
println("Hello world!")
}
}
I then try to compile it like so:
$ scalac HelloWorld.scala
And it compiles without any error messages. When however I try to run it like so:
$ scala HelloWorld
I get an error message:
No such file or class on classpath: HelloWorld
To implement this, I followed this tutorial and to solve the emerged error, I followed this suggestion. However,
$ scala objects.HelloWorld
also does not work. I know many people will now start heavily voting down my question and asking questions - have you ever tried to read some books on it (Yes, I did. I've read Horstman book for beginners, but it does not contain any information on compiling programs under Ubuntu). Still, I hope someone could help.
(This was a comment before, and I rephrased it to a response.)
You've done everything right, except for the last step: Use the java command instead of the scala command.
scala is the Scala REPL. No separate run command is required for Scala code, because it compiles to regular Java bytecode.
So try: java HelloWorld
For more complex programs that make use of the Scala library however, you need to include the Scala runtime library in the classpath. So, on the long run, it is beneficiary to use a tool like SBT, as pointed out by #roterl in the comments.
The answer saying that the scala command is just for the REPL is incorrect. You can see from the man page entry for scala (http://www.scala-lang.org/files/archive/nightly/docs-2.10.2/manual/html/scala.html) that it is intended to be used in the same way as the java command with the added flexibility that it will run the REPL, scripts, or compiled applications.
As some of the comments have indicated, this is almost certainly a path issue, which means that it requires more information to diagnose. One thing you can check is whether the scalac command produced a .class file in your current directory. If that is in the directory where you are running scala then the comments about needing . in your classpath are almost certainly correct.

How do I distribute a Scala macro as a project?

Suppose I have a Scala compile-time macro that I find useful and would like to share it (I do). How do I create a JAR file that when loaded into another project would execute the macro when compiling the new project?
Specifically, I've made a StaticAnnotation that rewrites the AST of the class that it wraps before compile time. This works in my Maven build (macro defined in the main directory, runs on test cases in the test directory) because I have
<compilerPlugins>
<compilerPlugin>
<groupId>org.scalamacros</groupId>
<artifactId>paradise_2.10.5</artifactId>
<version>2.1.0-M5</version>
</compilerPlugin>
</compilerPlugins>
in my scala-maven-plugin. (I'm starting with a Scala 2.10 project and if it works, will provide both 2.10 and 2.11.)
But if I put the resulting JAR on a Scala console classpath, in a Scala script, or into another Maven project (without special compiler plugins), it simply ignores the macro: the AST does not get overwritten and my compile-time println statements don't execute. If I use the #compileTimeOnly annotation on my macro (new in Scala 2.11), then it complains with the #compileTimeOnly error message.
Do I really need to tell my users to add compiler plugins in their pom.xml files, as well as alternate instructions for SBT and other build tools? Other packages containing macros (MacWire, Log4s) don't come with complicated build instructions: they just say, "point to this dependency in Maven Central." I couldn't find the magic in their build process that makes this work. What am I missing?
If you're relying on a macro-paradise-only feature then yes, you do need to tell your users to add compiler plugins. See http://docs.scala-lang.org/overviews/macros/annotations.html . The projects you mention are only using the scala compiler's built-in (non-paradise) macro features, not macro annotations.

Does Scala having an interpreter give Scala projects the option to execute them either compiled or interpreted? [duplicate]

This question already has answers here:
The difference between scala script and application
(3 answers)
Closed 8 years ago.
I'm aware that scala has an interpreter and scala is statically typed. So I'm wondering if it is possible to execute scala projects in both Java and PHP style ?
Maybe you just need an interpreter to test your code? Then type scala to get interpreter and use :load command to load scala file.
I don't know exactly what is the PHP style, but yes, you can execute scala interactively, static typing is not a big issue here. If you need to exectue simple script that's easy (code from "Getting started in Scala"):
#!/bin/sh
exec scala "$0" "$#"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world! " + args.toList)
}
}
HelloWorld.main(args)
If you have .jar dependencies in your script than things got more complicated,because you need to pass this jar dependencies to the scala interpreter. here is example
As of current date this method doesn't allow you to modularize scripts into the multiple files, but here is workaround
If you have sbt project you can type console from sbt shell to get a scala interpreter with correct classpath and dependencies. Also sbt itself has a 'script' mode which works quite like groovy's embedded dependency menegment.
Also scala compiler is embeddable) This project helps to dynamically compile/recompile scala files and load them into the jvm.