Source From Here
Question
Suppose I have a Scala class and a Java class in a Java project and the scala class is like below:
How can I call it's main method from the main method of a java program which is present in the same project
How-To
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:
You can then call this from your Java main method as follows:
Question
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-To
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")
- }
- }
- class Foo {
- public static void main(String[] args) {
- Sam.main(args);
- }
- }
沒有留言:
張貼留言