Why the main method cant be loaded - eclipse

I would like to run the simple example mentioned below. Eclipse generates an error reading:
main class can't be found or loaded
Please let me know how to fix this error and describe why it happens.
Main.kt
class Main {
fun main(args: Array<String>) {
println("Hello, World!")
}
}

The issue with your code is that the main function is not static.
To make it static, either put it out of the class (the recommended way):
fun main(args: Array<String>) {
println("Hello, World!")
}
Or use #JvmStatic annotation:
object Main {
#JvmStatic
fun main(args: Array<String>) {
println("Hello, World!")
}
}
Note that statics can only appear in objects. However, I am not sure that JVM will recognize such main method. It seems to be working as well:

Related

Intellij - Run scala main file

I have installed Scala plugin and sbt executer in IntelliJ.
I have created a new Scala project and it created a build.sbt file.
In the project setting/Libraries I see a reference to the SBT I have on my computer.
I created a new Scala class with the following code:
class RunMe {
def main(args: Array[String]): Unit = {
println("Hello from main of class")
}
}
I can't seem to find a new type of run configuration to create for the scala class.
I don't see the green play button in the left column (IntelliJ Left Gutter)
What am I missing?
How can I configure a run configuration in the code?
Instead of a class with a static method, which is what you do in Java, you should use object in Scala:
object RunMe {
def main(args: Array[String]): Unit = {
println("Hello from main of class")
}
}
You can also mixin a trait called App instead of providing the main method:
object RunMe extends App {
println("Hello from main of class")
}
In both cases IntelliJ should pick the definition fine and offer you the green arrow to start the app.
I usually just mixin the App trait into my Runner object. Something like...
object RunMe extends App {
println("Hello from main of class")
}
should do the trick. Intellij should now pickup that this object is "runnable" and provide a "play" button as expected.

Can I compile a Scala file with a different name than the class?

Can I compile a Scala file with a different name than the class?
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
Hello.scala
Have you tried? Then you would notice that, yes, you can. Indeed you can place multiple classes in the same file, and therefore there is no restriction as to how the file must be called.

Why args are compulsory for main

If I use like below considering I don't need to take arguments, it doesn't detect for Scala in eclipse.
object HelloWorld {
def main(): Unit = {
println("Hello Scala!!!")
}
}
It works fine with args: Array[String]
object HelloWorld {
def main(args: Array[String]): Unit = {
println("Hello Scala!!!")
}
}
Well it's simply a convention on the JVM. You won't be able to invoke your object as entry point when running your program. For example, in Scala.js you have main() without arguments.
If you don't need the arguments you can mixin the App trait:
object HelloWorld extends App {
println("Hello Scala!!!")
}

Using an object inside a class in same scala file

The following is the example that I am working on,
package com.sandbox.scala
class ScalaOne {
def main(args: Array[String]){
com.sandbox.scala.sayHello.hello(); //This works
sayHello.hello(); //error: not found: value sayHello
}
}
object sayHello{
def hello(){
println("Hello from Scala");
}
}
As you may see the second call sayHello.hello() throws an error mentioned in the comment. Why is that? I assumed it should work.

Scala println not working with App trait

When I use the scala App trait, I can't get println to work.
This simple example prints as expected,
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
But once I introduce the trait it does not,
object HelloWorld extends App {
println("Hello, world!")
}
I get no errors but nothing prints to the console.
Did you compile it first (running scalac HelloWorld.scala)? See this comment: http://www.scala-lang.org/node/9483#comment-40627
Edited to add more explanation:
The first version actually was compiled. Scala files without an explicit main method are run uncompiled as scripts. That means that for your second version, the commands in the file are run sequentially, as though they had been entered into the interpreter--so, the object HelloWorld is created, but no method is called on it. There's more information about Scala as a scripting language here (scroll to Step 5): http://www.artima.com/scalazine/articles/steps.html
Add a line
object HelloWorld extends App {
/* code */
}
HelloWorld.main(args)
at the end of your file.
The Class defines the method but it need to be called too.
According to
http://www.scala-lang.org/api/current/scala/App.html
you want to do
object Main extends App {
Console.println("Hello World: " + (args mkString ", "))
}