Suppose I have a Scala class and a Java class in a Java project and the scala class is like below
class Sam {
def main(args: Array[String]): Unit = {
println("Hello")
}
}
How can I call it's main method from the main method of a java program which is present in the same project
Typically, main methods are static
in Java, and in an object
in Scala. This allows you to run them from the command line. Your code defines a class
, not an object
.
I'd suggest changing your Scala code to:
object Sam {
def main(args: Array[String]): Unit = {
println("Hello")
}
}
You can then call this from your Java main method as follows:
class Foo {
public static void main(String[] args) {
Sam.main(args);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With