Reputation: 6586
I have a module in intellij, where I can create main methods in java which run perfectly, but when I create them in scala and attempt to run them, I get the following:
Error: Could not find or load main class xxx
My project relies on both java and scala classes. What do I need to do to allow scala main classes to run?
EDIT:
As requested, here's an excerpt of the main class I'm trying to run with scala. I know there's nothing wrong with the code because it works when I initialize the code in intellij as a scala project. The problem here is that I started with a blank project, so I don't know what magic intellij did behind the scenes to make the scala main classes run properly.
object WebProxyServer extends Logging {
def main(args: Array[String]): Unit = {
// implementation
}
}
class WebProxyServer() {
}
Upvotes: 0
Views: 999
Reputation: 27356
The easiest way to run Scala code is to create an object
that extends App
, like this:
object Main extends App {
println("Hello World")
}
Scala will execute all the code in the object when it is created. (The arguments are available in the args
member of App
)
To run this from IntelliJ, select "Run->Edit Configurations", then click "+" and select "Application" and fill in the dialog. The drop-down for the "Main Class" parameter should include all objects that extend App
, so pick the class you want to run. Once this is done, your Main
should appear on the "Run" menu.
You can have multiple classes that extend App
and multiple items in the Run
menu. I use this to debug specific parts of the code from the IDE without having the run the main program.
You can also create an object with a main
method like this:
object Main {
def main(args: Array[String]): Unit = {
println("Hello World")
}
}
Once this is built you can add Main
as a "Main Class" in IntelliJ as described above. All objects with a main
method will also appear in the "Main Class" drop-down.
Both approaches are acceptable, but don't combine the two. That is, don't inherit from App
and then override the main
method in App
.
Upvotes: 4
Reputation: 1924
Make sure you're adding the main method to objects, not classes. The main method needs to be static. You can add a companion object, which is just an object with the same name as the class, to contain the main method if you want to add a main method to an existing class.
class MyApp {
// bad main method; will not run
def main(args: Array[String]): Unit = println("hello world")
}
// companion object to MyApp class
object MyApp {
// good main method; will run fine
def main(args: Array[String]): Unit = println("hello world")
}
Upvotes: 0
Reputation: 568
first try check the SDK configuration to your IDE and if this cause you error.
object WebProxyServer {
def main(args: Array[String]): Unit = {}
}
then try extends App
and override
your main(args: Array[String])
if needed
Upvotes: 0
Reputation: 1924
To add Scala code to an existing Java module in Intellij, right click on the module name (usually the top level folder in the project view), and click "Add Framework Support", then check Scala in your list of options.
To add Scala code to a new module in an Intellij project, goto File -> New Module, and choose Scala from the list of options.
If your directory structure looks like this:
src
|
|-main
|
|-java
| |-....java packages
|
|-scala
|-....scala packages
Then don't forget to right click your scala directory in the project view and choose Mark Directory As -> Sources Root
Upvotes: 2