Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build, compile and run a Scala project?

Tags:

scala

sbt

I would like to evaluate a Scala project that I have found on Github, namely TRank.

I found the build file build.sbt. I managed to install Scala and sbt via homebrew and then run the command sbt run on the project root folder. Doing so ended up with an error:

java.lang.RuntimeException: No main class detected.
    at scala.sys.package$.error(package.scala:27)

Now the project files are in the src/main/scala/io/mem0r1es/trank and when I try to compile via scalac or run sbt run there I get a bunch of errors about objects not being members of the base packaged i.e. object ranking is not a member of package io.mem0r1es.trank

I would greatly appreciate some help knowing how to run this Scala project.

like image 231
AhmadAssaf Avatar asked Sep 24 '14 13:09

AhmadAssaf


People also ask

How do I compile a Scala project?

Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | sbt. In the sbt projects section, select a project for which you want to configure build actions. In the sbt shell section, select the builds option. Click OK to save the changes.

How do I compile and run a Scala program?

Press "control o" to save the file and then press enter. Press "control x" to exit the nano editor. To compile the code, type "scalac hello_world. scala" and press enter.

What is sbt compile?

sbt is a popular tool for compiling, running, and testing Scala projects of any size. Using a build tool such as sbt (or Maven/Gradle) becomes essential once you create projects with dependencies or more than one code file.

How do I run a Scala program in terminal?

Executing Scala code as a script Another way to execute Scala code is to type it into a text file and save it with a name ending with “. scala”. We can then execute that code by typing “scala filename”. For instance, we can create a file named hello.


1 Answers

As you've already noticed the project is managed by sbt.

In order to package a project, i.e. creating a jar with all the project artifacts that are supposed to be distributed, you execute package.

> package
[info] Updating {file:/Users/jacek/sandbox/TRank/}trank...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 13 Scala sources to /Users/jacek/sandbox/TRank/target/scala-2.10/classes...
[warn] there were 1 deprecation warning(s); re-run with -deprecation for details
[warn] one warning found
[info] Packaging /Users/jacek/sandbox/TRank/target/scala-2.10/trank_2.10-1.0.jar ...
[info] Done packaging.

By default sbt manages sources under src/main/scala directory. That's where you could find an App object to run.

In sbt, run searches all the sources under src/main/scala for applications.

> help run
Runs a main class, passing along arguments provided on the command line.

If a project has no main classes, the error is printed out:

> run
java.lang.RuntimeException: No main class detected.
    at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 0 s, completed Sep 24, 2014 9:40:52 PM

It does mean that the project has no main classes, but there are other ways to "prove" that an API works properly - using tests.

Execute test to fire the tests:

> test
[info] Compiling 5 Scala sources to /Users/jacek/sandbox/TRank/target/scala-2.10/test-classes...
[warn] there were 2 feature warning(s); re-run with -feature for details
[warn] one warning found
[info] ANC_DEPTHSpec:
[info] An ANC_DEPTH ranker
[info] - should rank types properly
[info] - should not fail when no types are provided
[info] ANCESTORSSpec:
[info] An ANCESTORS ranker
[info] - should rank types properly
[info] - should not fail when no types are provided
[info] DEPTHSpec:
[info] A DEPTH ranker
[info] - should rank types by maximum depth
[info] - should not fail when no types are provided
Loading classifier from edu/stanford/nlp/models/ner/english.all.3class.distsim.crf.ser.gz ... [info] PreProcessorSpec:
[info] A PreProcessor
[info] - should remove boilerplate from HTML content
[info] - should leave intact textual content
[info] - should not fail with empty content
done [2.8 sec].
[info] NERSpec:
[info] A NER
[info] - should extract entity labels
[info] - should not fail with content without Named Entities
[info] - should not fail with empty content
[info] Passed: Total 12, Failed 0, Errors 0, Passed 12
[success] Total time: 4 s, completed Sep 24, 2014 9:42:09 PM

So, to learn the project you should review the sources of the tests (under src/test/scala) and a scaladoc that you can generate using doc task:

> doc
[info] Main Scala API documentation to /Users/jacek/sandbox/TRank/target/scala-2.10/api...
model contains 20 documentable templates
[info] Main Scala API documentation successful.
[success] Total time: 1 s, completed Sep 24, 2014 9:43:22 PM

You could also use console task to enter Scala REPL and play with the types yourself.

> console
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.

scala>

With the tests, the scaladoc and the Scala REPL, you should be all set to learn the API of the project.

like image 146
Jacek Laskowski Avatar answered Oct 01 '22 19:10

Jacek Laskowski