How do I set up databinder dispatch to use in Eclipse? - eclipse

I want to write some simple HTTP requests in Scala, but the Databinder Dispatch library only has instructions for sbt. As I'm a relative Eclipse newbie, can someone provide instructions on how I use it in my Scala project in Eclipse?
I'm using Scala 2.9.0final. If it's incompatible with Dispatch, is there an alternative HTTP request library?
http://dispatch.databinder.net/Try+Dispatch.html
Thanks!

The page you have linked to has instructions for trying out Dispatch using the sbt console. It is much easier to just do that on the command line, although if you are convinced to do this with Eclipse you can read your integration options.
If you want to set up a project and write some code that can be compiled that uses Dispatch, you should follow this guide.
Which shows you how to pull in dispatch as a dependency with either Maven or sbt. The main thing is that you want the dispatch + dependencies jars on your project classpath in Eclipse before you can start playing with it - sbt makes this easy in Scala and Maven for Java. So you should look around for how to do that in Eclipse to see your options.

Dispath is build on top of Apache HttpClient which is pure Java library (so can be used from Scala). But if you want to use Dispatch, you can:
Git clone the example
Install sbt
Run sbt update
Look into lib_managed/scala_${version}/compile dir
Write a sample scala script Script.scala:
import dispatch._
val h = new Http
val req = url("http://www.scala-lang.org/")
val handler = req >>> System.out
h(handler)
h(url("http://www.scala-lang.org/") >>> System.out)
Run a script with proper class path. On linux you can do
scala -cp `echo lib_managed/scala_${version}/compile/*.jar | sed 's/ /:/g'` Script.scala
Enjoy!)

Related

How to run Scala 3 applications in the command line with Coursier

If you follow the steps at the official Scala 3 sites, like Dotty or Scala Lang then it recommends using Coursier to install Scala 3. The problem is that neither or these explain how to run a compiled Scala 3 application after following the steps.
Scala 2:
> cs install scala
> scalac HelloScala2.scala
> scala HelloScala2
Hello, Scala 2!
Scala 3:
> cs install scala3-compiler
> scala3-compiler HelloScala3.scala
Now how do you run the compiled application with Scala 3?
Currently there does not seem to be a way to launch a runner for Scala 3 using coursier, see this issue. As a workaround, you can install the binaries from the github release page. Scroll all the way down passed the contribution list to see the .zip file and download and unpack it to some local folder. Then put the unpacked bin directory on your path. After a restart you will get the scala command (and scalac etc) in terminal.
Another workaround is using the java runner directly with a classpath from coursier by this command:
java -cp $(cs fetch -p org.scala-lang:scala3-library_3:3.0.0):. myMain
Replace myMain with the name of your #main def function. If it is in a package myPack you need to say myPack.myMain (as usual).
Finally, it seems that is possible to run scala application like scala 2 version using scala3 in Coursier:
cs install scala3
Then, you can compile it with scala3-compiler and run with scala3:
scala3-compiler Main.scala
scala3 Main.scala
This work-around seems to work for me:
cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- YourScala3File.scala
This way, you don't even have to compile the source code first.
In case your source depends on third-party libraries, you can specify the dependencies like this:
cs launch scala3-repl:3+ -M dotty.tools.MainGenericRunner -- -classpath \
$(cs fetch --classpath io.circe:circe-generic_3:0.14.1):. \
YourScala3File.scala
This would be an example where you use the circe library that's compiled with Scala 3. You should be able to specify multiple third-party libraries with the fetch sub-command.

Import Twitter/chill to Scala code using Intellij IDE

I have to do some assignments with Scala and I'm a newbie for this language.
In the assignment, the prof requests me to implement serialization and deserialization, using twitter/chill.
https://github.com/twitter/chill/
However, I don't know how to import the libary into my IDE Intellij.
Each time I use val instantiator = new ScalaKryoInstantiator.
IDE notify me that: Cannot resolve symbol ScalaKryoInstantiator.
Could anyone can help me to resolve this issue?
Thanks and Best Regards,
Long.
One way to add a library in a Scala project, if you use sbt, is to add an sbt dependency. Most libraries and jars can be found and downloaded from the maven repositories: https://mvnrepository.com/artifact/com.twitter/chill_2.9.2/0.2.3
If you click the sbt tab on the page above, maven gives you a code snippet which can be directly pasted into your build.sbt. In order to compile and run your project using sbt, open the terminal in the directory where your build.sbt is located and use the "sbt run" command.
If you don't use sbt, download the jar from maven and follow the instructions in this answer: How to add external library in IntelliJ IDEA?

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.

Is the Akka Actors library installed with the Scala IDE for Scala 2.10?

I have recently begun exploring Scala, and have started by installing the Scala IDE in my copy of Eclipse (Indigo). I initially installed the Scala IDE for Scala 2.9, but then noticed that there was a newer release available for Scala 2.10. Installing the newer plug-in over the older one seems to have worked, but...
Scala 2.10 has deprecated the older Scala Actors in favor of Akka Actors. Thus I'm trying to add an import to my toy Scala project:
import akka.actor.Actor
This is flagged in the IDE with the error
not found: object akka
When I look at my Scala project's properties, I indeed do not see any of the akka-* jar files that are mentioned in the Akka documentation.
Do they need to be downloaded and installed separately, even though the Scala IDE plug-in installed the rest of Scala 2.10? Or have package names changed as part of integrating Akka actors in place of the older Scala Actors? (The documentation doesn't say so, but the Scala 2.10 release is fairly recent...)
No, they aren't packaged together.
The easiest way to make sure the Eclipse IDE can see your dependencies (Akka, and anything else referenced in your build.sbt file) is to let sbt do it using the sbteclipse plugin. Here's the instructions I wrote up for co-workers:
Install the "sbteclipse" plugin
This plugin will allow sbt to add the files/references that Eclipse needs to find all the dependencies that you specify in your build.sbt. Otherwise, you will be able to use the IDE, but you will seek all kinds of "object not found" errors.
Just make sure the plugin is being added in your global plugins.sbt file. This file (and it's path) may not exist so you may need to create it at the following location:
~/.sbt $ cd ~/.sbt/0.13/
~/.sbt/0.13 $ mkdir plugins
Edit/create the plugins.sbt file:
~/.sbt/0.13 $ vi plugins/plugins.sbt
then add this line (it may be the only line in the file):
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "2.5.0")
Running sbteclipse
To use this, you just navigate to a scala project on the commandline and run the following. If you already had Eclipse open, go ahead and restart it.
/sites/ewuser (master)$ sbt eclipse
References:
How to initialize a new Scala project in sbt, Eclipse and github
Official sbteclipse plugin
The Akka artifacts are not bundled with the Scala IDE (yet), you will have to add “akka-actor_2.10” and friends to your project’s dependencies.
Download the akka for eclipse from below location
http://downloads.typesafe.com/akka/akka_2.11-2.4.1.zip?_ga=1.167921254.618585520.1450199987
extract the zip
add dependencies from the lib folder into project

Scala : trying to get log4j working

Scala newb here (it's my 2nd day of using it). I want to get log4j logging working in my Scala script. The script and the results are below, any ideas as to what's going wrong?
[sean#ibmp2 pybackup]$ cat backup.scala
import org.apache.log4j._
val log = LogFactory.getLog()
log.info("started backup")
[sean#ibmp2 pybackup]$ scala -cp log4j-1.2.16.jar:. backup.scala
/home/sean/projects/personal/pybackup/backup.scala:1: error: value apache is not a member of package org
import org.apache.log4j._
^
one error found
I reproduce it under Windows: delimiter of '-classpath' must be ';' there (not ':'). Are you use cygwin or some sort of unix emulator?
But Scala script works anywhere without current dir in classpath. Try to use:
$ scala -cp log4j-1.2.16.jar backup.scala
JFI: LogFactory is a class of slf4j library (not log4j).
UPDATE
Another possible case: broken jar in classpath, maybe during download or something else. Scala interpreter does report only about unavailable member of the package.
$ echo "qwerty" > example.jar
$ scala -cp example.jar backup.scala
backup.scala:1: error: value apache is not a member of package org
...
Need to inspect content of the jar-file:
$ jar -tf log4j-1.2.16.jar
...
org/apache/log4j/Appender.class
...
Did you remember to put log4j.jar in your classpath?
Had Similar issue when started doing Scala Development using Eclipse, doing a clean build solved the problem.
Guess the Scala tools are not matured et.
Instead of using log4j directly, you might try using Configgy. It's the Scala Way™ to work with log4j, as well as configuration files. It also plays nicely with SBT and Maven.
I asked and answered this question myself, have a look:
Put it under src/main/resources/logback.xml. It will be copied to the right location when SBT is doing the artifact assembly.