There is "Run Configuration" but no "Run As Scala Application" in Eclipse - eclipse

I have the following Scala class, I am able to run it from command line
by first typing sbt then in sbt mode. But I am not able to run it from eclipse .
I am already in scala perspective.
> run-main bcomposes.twitter.QuerySearch #IPL
package bcomposes.twitter
import twitter4j._
import collection.JavaConversions._
/**
* Gets a Twitter instance set up and ready to use.
*/
trait TwitterInstance {
val twitter = new TwitterFactory().getInstance
}
/**
* Given a command line query, search for tweets and print
* them.
*/
object QuerySearch extends TwitterInstance {
def main(args: Array[String]) {
val statuses = twitter.search(new Query(args(0))).getTweets
statuses.foreach(status => println(status.getText + "\n"))
}
}

Make sure the package name you specified under which your scala object file is added matches the package name you mentioned in your code. So, correct this and then check, "Run as Scala Application" should be available now

Your Scala file has to be a part of Scala project.
Create Scala project using Scala Project wizard, add your file to it and try again.

Try to click right button on the Scala object with main method (not on the root project element). Then the "Run as... Scala application" menu item appears.

"Run as Scala application" will show in Eclipse Scala IDE only if we extends the Scala object with 'APP' class.
Example:
package grreter
object Exp extends App{
println("Hello, New World!")
}
(Note: if you comment "extends App" then 'Run As Scala Application' will not show.)

The way I solve this problem is using maven to compile and package the project , when the maven build is correct, the "run as scala application" appear again.

Related

scala test eclipse plugin does not discover test resources

I have a scala project which uses sbt for building/testing/etc. I have a file with unit tests in:
$SBT_PROJECT_ROOT/src/test/scala/foo/bar/SomeSpec.scala
I also have a test resource in:
$SBT_PROJECT_ROOT/src/test/resources/some_test_resource.txt
I attempt to acces this file from the unit tests with:
import org.scalatest._
import scala.io.Source
class TestFiddleParser extends FlatSpec with Matchers {
"This unit test" should "find the test resource" in {
val source = Source.fromURL(getClass.getResource("/some_test_resource.txt"))
val content = source.mkString
println(content.take(1000))
}
}
When I am on the commdand line in the $SBT_PROJECT_ROOT folder and run the command:
sbt test
I can see the first 1000 characters of the test file being printed. Success!
Now I am using eclipse (Scala-IDE) for devolpment. I have eclipse support through sbteclipse (https://github.com/typesafehub/sbteclipse) and I run unit tests from within eclipse using the ScalaTest eclipse plugin (http://www.scalatest.org/user_guide/using_scalatest_with_eclipse).
When I run this unit test from within eclipse, I get a null in the val source. Which I believe means the resource was not found.
What could be the problem?
Ok it was actually quite obvious, just run
sbt eclipse
again AFTER adding the the test resource to the src/test/resources folder.

scala main method execution from command line in maven project

I am new to scala. Any help will be much appreciated.
I have created a maven project having scala files in a package in ScalaIDE editor. I want to execute main method implemented as mentioned below in one of the scala file lets say TestHelloWorld.scala
object TestHelloWorld {
def main(args: Array[String]): Unit = {
println("hey .. Hello world !!!")
}
}
I can run it from eclipse run as scala application and print statement be there on eclipse console. But I do not want to run it from IDE.
Please tell me, how should I be running this from command line in a general way, so that it gets executed?
People have suggested to run it through scala prompt "scala > ", but I want to run it from normal console.

How to declare main class for Scala in IntelliJ IDEA?

I'm trying to implement the simplest bits of coding in IntelliJ IDEA 2016.2.1 with Scala plugin, scala-sbt 0.13 and jdk-1.8. Here's the code:
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
It compiles with an error
Error: Could not find or load main class HelloWorld
In Run/Debug configurations the're is a warning 'Class HelloWorld not found in module ...'
Could you bring some light on that?
Right click the Scala project in IntelliJ IDE and click "Open Module Settings". Now, click "Global Libraries" and see whether Scala libraries are present there. If there is no Scala libraries, please add it.
Please refer the below snapshot.

trouble with configuring scala project in intellij

I have been following the getting started with IntelliJ and Scala video from JetBrains and running into two problems.
I can't get it create or start a run configuration
I don't see the scala-test library as a selection under ProjectStructure-Modules-ChooseLibraries
What I've done so far is
Install Scala, add path and environment variables
Install Scala intellij plugin
Create new project set project sdk to java 1.7 and scala home to /usr/local/share/scala-2.10.3
Create an object that extends from App with a simple write line:
The one source file object
object HelloWorld{
def main(args: Array[String]) {
println("hello")
}
}
In the video they right click on the object file and can see a selection of run, but in my case I only see run as Scala Console. I can't seem to get the debugger to work and when I try to create a run configuration as an "Application" it says the src file is "not acceptable"
I'll recommend my simple project skeleton, to get you quickly up and running with Intellij, SBT and even Eclipse setup. Hope it helps!

Running Scala Files in Eclipse

I'm trying to run a scala file with Eclipse Indigo. I have installed the Scala IDE for Eclipse and am on the Scala perspective, but the option to run as a Scala application does not appear. The file that I am currently trying to run is part of an existing project that I imported into Eclipse. The Scala IDE plugin appears to be working as I can create Scala objects and run them as Scala Applications and the Scala compiler is detecting errors.
How do I get the option to run a file as a Scala Application for the Scala IDE plugin for Eclipse Indigo?
The file you are trying to run must either be defined like this:
object MyFile extends App { /* your code to run here */ }
or:
object MyFile {
def main(args: Array[String]) { /* your code to run here */ }
}
If either of these are true, you should be able to press the play button, via right click or the toolbar at the top.