I am trying to get a main method to run in Scala in Intellij, but it is not working, and I don't know why.
File structure looks like this...
.
├── cse116.iml
├── pom.xml
├── src
│ ├── Main.scala
└── target
└── classes
Main.scala looks like this...
object Main {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
When I try to do "Run" on Intellij, I get the following error:
Error: Could not find or load main class Main
I am not sure what configuration is messed up. What may be some common reasons for this happening?
Try to right click on your mouse when you are in Main.scala file and select "run 'Main.main()'" option with green triangle
Assuming you are running a recent version of intelliJ, have the Scala plugin installed and your dependencies for Scala is correct in your pom file. You should see a tiny green arrow either on the left hand side of your main class or when right clicking within that class. Also, if you are on macOS there is the ctrl+shift+r to run the file as shown in the image below.
If that is not the case, consider creating a new Scala project from the IDE itself. You'd then choose an sbt project. sbt is the equivalent of maven in the Scala world. This one "should" work, if it doesn't it is very likely to give you a better Scala specific error message(s)
Related
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.
I'm sure I've seen a page that shows how to put SBT instructions inside a scala file, instead of build.sbt. The advantage is that everything then lives in a single file, which makes it easier to use Scala as a scripting language.
But I can't now find the example... can anyone help?
BTW, I know about ammonite, but it has limitations compared to SBT.
I think you're looking for this page of the sbt docs: Scripts, REPL, and Dependencies. There are also some other resources on the topic:
an example project by Eric Richardson with detailed instructions: ekrich/sbt-scripting
a post by Eugene Yokota (maintainer of sbt): scripting with Scala (it's from 2014, so I'm not sure if it's still relevant)
an example script gist by Seth Tisue
You can create your customize scala file under project/ directory and import it in build.sbt, like:
├── build.sbt
└── project
└── Settings.scala
so you can import it in build.sbt:
import Settings._
I created a new SBT project HelloScala, then I created a package called week6 and then a Scala worksheet under it. Eventually, I got the following directory structure:
~/HelloScala/.idea
~/HelloScala/project
~/HelloScala/src/main/scala
~/HelloScala/src/main/scala-2.12/week6/hello.sc
However, when I put package week6 in the worksheet hello.sc, IntelliJ warns that Project names doesn't correspond to directories structure. My questions are:
What should be the correct directory structure IntelliJ/SBT expects? Is it specified in some .xml file?
Notice that IntelliJ created two scala subdirectories, scala and scala-2.12. Is it correct? Or IntelliJ somehow failed to recognize the scala which had already existed but download and install another scala?
Thank you!
I don not use IntelliJ that often, but usually Scala packages should go into src/main/scala.
The structure of a Scala project is nothing too different from a Java one. Minimally you will need:
myproject
├── build.sbt
└── src
└── main
└── scala
└── mypackage
which is a project with a single package, a SBT script and nothing else.
For a more complex example, a Scala project could look like:
project
├── build.sbt
├── project
├── target
└── src
├── main
│ ├── java
│ ├── resource
│ └── scala
└── test
├── java
├── resource
└── scala
in which the project folder is used by SBT for various purposes, the target folder contains the compiled .class files and .jar packages.
To answer your questions:
The above structure is used implicitly by the SBT plugin. Maybe it is customizable, but in most cases it is suggested against doing this.
Your Scala source code should go into src/main/scala. I have no idea where does the other directory come from.
#sgu It seems I misunderstood your question and unfortunately I cannot leave a comment. The file "hello.sc" you are trying to deal with is treated by IntelliJ as a "Scala worksheet". I guess it is a REPL behind IntelliJ which evaluates as you edit. However it is not treated the same as a "Scala source file", so adding package xxx gives you the warning. If you want to create a package, the source files should be in ".scala" extension.
You should put all your source code under "main/scala" directory. "Scala-2.12" is the directory, that would be created with sbt under "project/target" after you compile/build it.
Have you tried clicking the Make project option on top of the scala sheet in Intellij Idea? I use that option when I need to import oher packages into one scala worksheet.
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!
I use SBT 0.11.0. It requires that all sources must be placed in:
[project]/src/main/scala/
However I want a custom package such as:
[project]/src/com/test/...
I can leave things as SBT requires, then specify in every source file the custom package:
package com.test
But I got some problems with Eclipse auto-features such as import statements, generating new classes... Eclipse always adds main.scala before com.test. Is there something I can do to solve this?
Edited
For example I have this:
[project]
src
main
scala
com
test
A.scala
package com.test
...
B.scala
package com.test
...
In B, I use some functions of A, I type it, then press Ctrl+Shift+O to let Eclipse import A. Eclipse does this:
import main.scala.com.test.A
But in A I set package to com.test (not main.scala.com.test).
This is small example. But when I have many source files, I need to refactor my code, things will be harder...
You are confusing source folders and packages!
sbt by convention uses the source folder src/main/scala for Scala code. Within this folder you can create whichever packages you want, e.g. a subfolder foo/bar with a file Baz.scala that contains the following code:
package foo.bar
object Baz {
...
}
If you use the eclipse plugin for sbt then you can tell sbt to generate an Eclipse project which has all the details configured to work correctly with sbt's project structure. All the details you need to add it to your sbt project and to run it are at the link above.