Class Not Found running Scala in IntelliJ SBT project - scala

Apologies as this is a newby question. I have the following SBT project containing a scala object. When I run 'Hi' I get "Class not found" exception. Where to specify the class path?
click here to see the screen shot

You need to delete this object and create a new one. Right click on src->main->scala(NOT in target folder) folder new -> Scala class -> type "Hi" in name field in check box choose object. And paste your:
def main(args: Array[String]) = println("hi")

This could be because main method doesn't have return type, try adding Unit so I must be like this:
def main(args:Array[String]):Unit = println("hi")
regards

Related

Unable to run Scala application in IntelliJ

I'm trying to run a simple Scala snippet,
package example
class HelloWorld extends App {
println("Hello world")
}
in the IntelliJ IDE with Scala installed. However, the "Run" button appears to be grayed out, and I also don't see it in the context menu (not shown in the screen grab below).
In accordance with the answer of Unable to run Java code with Intellij IDEA, the code is in the src folder which is marked blue. (I've also tried marking it as a 'tests' folder but to no avail). What am I missing?
HelloWorld should be object, not class:
package example
object HelloWorld extends App {
println("Hello world")
}
For more info about singleton objects, you can see this chapter of "Programming in Scala" book and this question.
To run any scala application you need singleton object which will either extend the App or define the main method that takes one parameter, an Array[String], and has a result type of Unit.
Any standalone object with a main method of the proper signature can be used as the entry point into an application.
So you can run your scala application in following two ways.
package example
object HelloWorld extends App {
println("Hello world")
}
object HelloWorld {
def main(args: Array[String]) {
println("Hello world")
}
}

InteliJ IDEA,create a Scala class,proper order

I am learning from Coursera tutorial,but my IDEA is different from the one in the course.This is the screenshot
When I try to create class,I got this
What should I change to get object instead of class?
This is how my GUI looks like
When you ask to create a scala class, select the object from dropdown
1) Create scala class
2) Select kind of scala class
but you will have to add main method by yourself, or also can extend App.
object ExampleApp {
def main(args: Array[String]): Unit = {
}
}
When you open the dialog for new class, do not click "OK" and below the name of file, select dropdown "kind" to select object or traits.

Intellij Scala- unable to find Scala App

I just set up Scala in Intellij(along with SDK and JDK)
File -> Project -> Scala -> Scala
Created a project.
under project name src --> right click --> I can see 1.Scala Class , 2.Scala Worksheet , 3.Scala Script , But the Scala application or App option is not coming .
what am i doing wrong here. Please help
Just like in Java you should create a class, that has main method, that can be found by java machine and get run. The searched method has signature
In java:
public static void main(String[] args){
//your code goes here
}
In scala:
def main(args: Array[String]): Unit = {
//your code goes here
}
Also in scala you can extend from App(scala.App, all names from package scala._ are imported by default):
object Main extends App{
//your code goes here
}
That moves your code into automatically created function def main(args: Array[String]): Unit. This option is "faster", but limited in some functionality.
So, click "Scala class", pick object, add "extends App" clause or write "def main ..."
You need to have a main method in some object to make it "runnable":
object test extends App {
println("Hi");
}
or
object test {
def main(args:Array[String]):Unit = println("Hi");
}
Extending App creates a main method under the hood for you, but messes with initialization.

How do I use an unmanaged dependency in this simple Play example?

I am trying to write a Scala Play web service that returns JSON objects and am having trouble calling a function in a dependency. Can someone tell me what I'm doing wrong in this simplified example?
I have a project called SimpleJSONAPI that consists of the following object.
package com.github.wpm.SimpleJSONAPI
import play.api.libs.json.{JsValue, Json}
object SimpleJSONAPI {
def toJson(s: String): JsValue = Json.toJson(Map("value" -> s))
}
Unit tests confirm that given a string it returns a JSON object of the form {"value":"string"}.
I have a separate Play 2.2.3 Scala project that I created by typing play new PlayJSON. I added the following json action to the controller in the generated application.
package controllers
import play.api.mvc._
import com.github.wpm.SimpleJSONAPI._
object Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
def json = {
val j = SimpleJSONAPI.toJson("The JSON API")
Action {
Ok(j)
}
}
}
I also added this route.
GET /json controllers.Application.json
In the root of the PlayJSON project I have a lib directory that contains the simplejsonapi_2.11.jar built by SimpleJSONAPI. This appears to contain the correct code.
> jar tf lib/simplejsonapi_2.11.jar
META-INF/MANIFEST.MF
com/
com/github/
com/github/wpm/
com/github/wpm/SimpleJSONAPI/
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI$.class
com/github/wpm/SimpleJSONAPI/SimpleJSONAPI.class
This compiles, but when I try to connect to localhost:9000/json I get the following runtime error in the line with the val j assignment.
java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(L/java/lang/Object;)Ljava/lang/Object;
I've also seen the same error in a unit test that exercises the /json route with a FakeRequest.
If I copy the toJson function from the external dependency into the Play application everything works.
As far as I can tell from the documentation I'm doing everything right, and the error message is opaque. Can someone tell me how to get this to work?
I think your import is incorrect given how you are using the API. Either exclude the object name on the import...
import com.github.wpm.SimpleJSONAPI._
Or change your usage to drop the object name...
val j = toJson("The JSON API")
This was a problem with Scala compiler version compatibility. I compiled my SimpleJSONAPI dependency with Scala 2.11, while the Play app was being built with Scala 2.10. When I changed the SimpleJSONAPI dependency to also build with Scala 2.10, I was able to use it in my Play app.
This was confusing because it's not obvious from the project files which version of Scala a Play app is using, and the error message about ArrowAssoc gives no indication that it is a compiler version issue.

Using IntelliJ for a Scala Project

I am missing something simple here.
Scala downloaded
Scala home set
IntelliJ plugin downloaded
When new module is added, scala is chosen:
When new class is created, however, when trying to run it, i get
Looking as module properties, i see
What am i missing please?
Two things:
The file you're working with must be a Scala class.
You should have an object declared somewhere (preferably outside of the class).
object runnableObject {
def main(args: Array[String]) {
println("Hello world!")
}
}
You can then use this object to run your Scala code in IntelliJ.