New scala project in Intellij - Error: Could not find or load main class - scala

I just created a simple helloworld scala project in Intellij. My main class is simply printing Hello World -
package xx.yy.zz
object HelloWorld{
def main(args: Array[String]): Unit = {
System.out.print("Hello World!")
}
}
My project structure
scala-test
|_src
|_main
|_scala
|_xx
|_yy
|_zz
|_HelloWorld
In the project structure settings, I have "scala" as my source folder. I am able to select my main class correctly in the application run configuration I created. So far so good. After this, when I try to run my run configuration, I get the dreaded error -
Error: Could not find or load main class xx.yy.zz.HelloWorld
At this point, I have tried all the things I could find on google like invalidating cache, reloading project, switching source folder to be "src" instead of "scala" and many others that I have lost track of by now. Please help!!

I didn't investigate the reasons for this issue but as a workaround, you can compile the project via sbt/shell sbt before running via Intellij.

Related

Problems compiling and running Scala projects in IntelliJ Idea

I have installed the latest IntelliJ Idea vertion with the Scala plugin. I have created a SBT Scala project with the IDE.
Unfortunately, I am not able to compile or run even the simplest "Hello World" example.
object Main {
def main(args: Array[String]): Unit ={
val x: Int = 5
println("Hello Scala!")
println(x)
}
}
If I try to run it, it says that "Error: could not find main class Main".
Rebuilding the project does not help.
BUT, if I run the "sbt" terminal program and execute "run", everything runs fine. Even more, it compiles the necessary class file so that IntelliJ Idea is able to run it after this step.
But, whenever I change something in the code and try to rebuild it from Idea, it will fail as before.
Edit: in Eclipse everything runs ok.
UPDATE
If I try executing "compile" from the sbt shell and then "Run" with the IDE, it will work. But, it will not do it while executing "Build" or "Rebuild Project". Sometimes, but unfortunately not always sp as to be able to reproduce it, it will throw me an exception with "Could not initialize class sbt.internal.io.Milli$"
UPDATE 2nd:
I have simplified even more the task. Now I have something like the following screenshot:
Screenshot IntelliJ IDEA
UPDATE 3rd:
There were two errors in my deployment. One of them was that, as it was pointed before, there was some package definition problems. All the source code should depend on src/main/scala, but that is not enough. So as to use the SBT structure for construction, I had to go to
File -> Settings -> Build, Execution, Deployment -> Build Tools -> sbt
and check "Use auto-import" and "use sbt shell for build and import".
After that, everything runs ok. Finall!
Thanks a lot tro everyone for the useful input!
On your screenshot Runner is in wrong package. It's written package main.scala.
If Runner.scala is in src/main/scala/way/to/my/package package declaration should be package way.to.my.package (if it's just in src/main/scala there should not be line package ...).
Also if still necessary you can try (from what should be tried first to what should be tried last, if things tried before didn't help)
sbt clean
reimport the project to IntelliJ IDEA
File -> Invalidate Caches / Restart ...
delete .idea subfolder of project folder
Did you try the green arrow near "object Main"?
This should start the program.

Facing issue while trying to enable source code in IntelliJ for scala

I am new to Scala programming and I have just started with basic programs in scala in IntelliJ IDE.
When i use the below code
package com.allaboutscala.chapter.one.tutorial_04
object HelloWorld extends App{
println("hello from hello world")
}
I wanted to see the source code of App where it uses the main method, but when i tries to see the source code and when I try to download it, I am getting error like
Sources not found: Sources for 'scala-library.jar' not found. I have attached the screenshot also.It would be great if someone could guide me here.
Since you are using sbt, you need to enable downloading of sources for your project:
open IntelliJ Preferences
search "sbt" or navigate to Preferences | Build, Execution, Deployment | Build Tools | sbt
select "Download: library sources"
refresh the sbt project

How to build a play project in Intellij IDEA 2016.2.2?

I have created a new play project in Intellij IDEA 2016.2.2 according to the tutorial: https://www.jetbrains.com/help/idea/2016.2/getting-started-with-play-2-x.html (the section "Creating a project"). As a result I have got a project with file Application.scala containing:
package controllers
import play.api._
import play.api.mvc._
class Application extends Controller {
def index = Action {
Ok(views.html.index("Your new application is ready."))
}
}
Then I tried to build it using Build -> Rebuild project command. But I get build error:
Error:(9, 19) object index is not a member of package views.html
Ok(views.html.index("Your new application is ready."))
After starting the app using Run -> Run 'Play2Run' and including the target folder to the build process I managed to build the project via Build -> Rebuild project. But what should I do before building in the case I haven't launched the app via play?
Explanation why my question is not duplicate of another question:
I haven't found clean project opportunity in IntelliJ. Invalidating cache of IntelliJ doesn't help as well. Also the directories target/scala-2.11/src_managed/main and target/scala-2.11/twirl/main are absent in this new project. They emerge after Run 'Play2Run' and after that I can configure this Java Build Path, but I want to avoid using Run 'Play2Run'. How can I create these directories without the Run 'Play2Run'?

Could not find or load main class in eclipse using maven and scala

I am using the Scala IDE.
I have created one Scala project and wrote simple scala object i.e.
object Test {
def main(args: Array[String]): Unit = {
eprintln("hi")
}}
It's running as expected.
After that I have converted my project to a maven project.
I did Clean and install.
after that if I am trying to run my scala class, I am getting below error:
Error: Could not find or load main class Test
How can I solve this?

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!