Can't debug a Scala application in IntelliJ + sbt-idea-plugin - scala

I'm developing a Scala application using IntelliJ Idea 12, sbt and sbt-idea plugin. I generated all necessary files by saying "gen-idea" and everything goes well -- I was able to compile the sources in command line in sbt as well in IntelliJ Idea 12 itself using menu Build->Make Project.
Here is my main class (Application.scala)
object Application extends App {
val a = 12345
println("application entry point 1235")
}
I have a breakpoint at val a = 12345.
1) There were other errors before, but now says "Module is not specified". How do I solve this?
I'll be posting updates since there would be other errors as my gut tells me.

To debug just like to run, you need to create a run configuration (menu Run -> Edit Configurations). If you haven't done yet, you need to add an Application entry with the + button.
Not only do you need to specify the main class, but also which "module" that class belongs to. By default, "Use classpath of module" will be empty. Here in the popup menu, you need to select the main module (not the one ending in "-build"). After you choose that and close with "Ok", it should work.
Although not necessary, I also recommend to use sbt for building instead of "Make". In the configuration in the "Before launch" part, select "Make" and click on "-", then click on "+" and choose sbt -> test:products.
Edit: Here is the reference for the SBT plugin for IntelliJ.

Related

how to compile and run one single scala file without building whole project within intellij

I have several scala object in my project, sometimes I would like to run one single object whch has no dependency on any other object in the file (just want to see how it would behave before merging it into the whole project), but every time I click on "Run 'XXX'" (where xxx is the name of the object I would like to run, "hello world" for example), intellij tried to build the whole project, actually I just wanted to build and run the single scala object, like "helloword.scala".
I searched a lot and mostly the questions are about Java, if someone wants to run single Java class, then he can right click in the editor zone, then choose the "Run xxx.main()", but in my case ,I just can find the "Run xxx",
someone said that cancel 'make' in run/debug configuration, but how and I didn't find 'make' button in the configuration.
so, anybody have an idea how to compile/build/run just one single scala object/file at one time in intellij?
BTW I am using the intellij idea 2019.1 and the project is based on maven.
Try removing Build action from Before launch section of the corresponding Run/Debug Configuration like so
Edit the Run/Debug Configuration for the Application configuration type
Go to Before launch section
Remove Build action by clicking on the minus - button
You can test this out by creating two apps
// Run.scala file
object Run extends App {
println("woohoo")
}
and
// RunBroken.scala file
obct RunBroken extends App {
ptln("boom")
}
Create run configuration for Run.scala by following above instructions, and it should run despite there being a syntax error in RunBroken.scala.
You can create a Scratch File.
Simply search for Scratch file in the actions search
And search for Scala in the languages dialog: (I'm using Java as an example, since I don't use Scala.)
This will create a new scratch which is accessible under Scratches and Consoles -> Scratches in the Project view.
As you can see here, there are Syntax Errors in a Project file
and yet, the Scratch file will compile and run:

IntelliJ: Scala worksheet don't pick up code changes without restart

If I have the contents of an object imported at the top of a Scala worksheet in Intellij, when I add new things to the object, they cannot be found in the worksheet. Even after I build the project and reevaluate the worksheet, it can't find the new property. Autocomplete works. If I quit and restart IntelliJ it works.
This is the error:
java.lang.NoSuchMethodError: Arith$.foo()Ljava/lang/String;
at #worksheet#.get$$instance$$res6(ArithWS.sc:15)
at A$A1$.main(ArithWS.sc:41)
at #worksheet#.#worksheet#(ArithWS.sc)
Weirdly, it seems to compile, but fails at runtime. This is a normal SBT project. IntelliJ 2017.2.5, Scala 2.12.3. Selecting the "Make project" checkbox doesn't help. Actually no combination of checkboxes makes it work. No amount of rebuilding the project or the worksheet helps either. Only restarting IntelliJ will get the worksheet to pick up the new code.
Example project code in Arith.scala:
object Arith {
val foo = "foo"
}
Example worksheet ArithWS.sc:
import Arith._
foo
This seems to fix it, though I would love to know why the default setting is broken, or what this undocumented setting does: Go to File -> Settings -> Languages & Frameworks -> Scala -> Worksheet (tab) . Unselect "Run worksheet in the compiler process".
In case others end up here as bewildered as I was and find that the above doesn't resolve the issue, try unchecking the "Use external compile server for scala" tick box in Preferences > Build, Execution, Deployment > Compiler > Scala Compiler > Scala Compile Server (you can also get to this view from the speedometer-like icon in the bottom right of Intellij).
Combined with "Make project" on the Scala worksheet view, this seems to ensure that changes are properly compiled and visible from within the worksheet.

Scala: Intellij shows compile errors on Import statements

I'm trying to teach myself scala and I'm using IntelliJ IDEA 2016.1.3. The editor does not recognize import commands. Errors appear. I have downloaded libraries, but the problem persists. Am I doing something wrong?
Try this,
1.) I created a scala project:
In your console:
$ activator new my-first-app play-scala
Please bear in mind that I just automate the creation of scala project which follow the conventional project structure. I am using the Play framework and activator command, fyi, but this is out of topic.
2.) I open the project folder in IntelliJ by
a. Go to File -> New -> Project from existing source
b. find the "my-first-app" folder
c. double click the "build.sbt"
d. a prompt appear and check the "Use auto-import" checkbox
e. make sure the project sdk is a JDK 1.8
f. Click OK.
g. Select the modules data as root. As is.
h. Go to Settings (Ctrl + Alt + s)
I. Under "Languages & Frameworks", go to "Play2"
j. In the Play2 section, check the "Use SBT watcher" and "Use Play 2 compiler for this project"
h. Click "apply" then click "ok"
g. if still not work, refresh the project by going to SBT child window of intelliJ and click the refresh button.
Good luck! :D

Scala running issue on eclipse

I configured everthing within eclipse for scala. I create a snippet to show you the issue, i can't see in run options run as scala application, i also tried to find my main class under build configuration option but i can't find it.
How i can solve it?
to run as scala application, you need to create Scala App and not class
In eclipse, package explorer
select project/src/package
right click
new>scala app
inform Name e.g. Test and click "finish"
select Test.scala
right click
"run as Scala Application"
see results in console window.
Replace class with object.
You can even have it extend Application so it can look like:
object afaf extends Application {
println("Done.")
}

How to start with Scala, intelliJ IDEA and SBT?

I downloaded IntelliJ IDEA 14.0.3, SBT, Scala and SBT plugins, set environment variables correctly, i guess (if i can run Scala repl is it correct?)
When i start IntelliJ IDEA and choose new SBT-based project i get some problems:
1) no /src/ folder in project structure. How i can fix that?
2) can't compile manually created Scala file: IDEA suggest me edit configuration with Alt+Shift+F10 then i get window with various settings, so what directly should i edit?
Sorry for possibly stupid questions, but i just want to run miserable
"hello world" in Scala...
You don't need any extra plugins besides the Scala plugin. Assuming you have that installed, then
Step One: Fire up IntelliJ and Click on "Create New Project"
Step Two: Choose Scala from the left hand side panel and choose SBT on the right hand side panel (i.e SBT-based Scala project) and click Next.
Step Three: Fill in your project details and click Finish
Step Four: Wait for Intellij to download all SBT dependencies and to construct the folder structure and to index.
Step Five: Begin hacking!