28

I am new to Scala. I am only able to write basic code thus far, but I want to start using it more concretely, rather than just learning theory.

Let's say I have the following Java code in HelloWorld.java:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}

What would the code be to invoke it from Scala?

Nagev
  • 10,835
  • 4
  • 58
  • 69
frazman
  • 32,081
  • 75
  • 184
  • 269

3 Answers3

45

In your example, you just have a main, not a function you would necessarily call from somewhere else. But let's said you did have a function like

package com.example.hello;

public class HelloWorld {
  public static void sayHello() {
    System.out.println("Hello, world!");
  }
}

(I also added a package for your example, for completeness). Then in your Scala code, you could do:

import com.example.hello._

object GreetWorld extends App {
  (0 until 10).foreach {
    HelloWorld.sayHello()
  }
}

to say hello using the Java function 10 times in Scala. The ._ in the import imports all members of the package, or alternatively you could just import com.example.hello.HelloWorld. You could even import the method itself with import com.example.hello.HelloWorld.sayHello so that you don't need to reference the HelloWorld object in your code.

Both languages compile into JVM bytecode, so calling Java code from Scala is very simple, although calling Scala from Java can be trickier if there are are implicit parameters involved.

Nagev
  • 10,835
  • 4
  • 58
  • 69
arkajit
  • 797
  • 6
  • 4
  • Hi, May I request you to please write the code completely.. I am very new to scala.. My scala codeblock is inside the "object" definition.. and rest of the stuff is same.. Does scala and java files should be in same folder?? – frazman Mar 19 '13 at 18:47
  • 1
    I am getting this error : helloworld.scl:1: error: object example is not a member of package com import com.example.hello – frazman Mar 19 '13 at 18:52
  • @Fraz if you still have the problem, see changes above – Luigi Plinge Mar 20 '13 at 00:06
  • `(0 to 10)` is 11 times, not 10. the range is inclusive. – sam boosalis Apr 17 '14 at 05:02
  • Maybe the code was updated, but it says 0 until 10, not 0 to 10, and until is not inclusive. – Davos Jun 16 '17 at 21:45
  • @samboosalis was in a hurry it seems. Didn't read it right. No, the code hasn't been modified since the answer was posted. – Gautam Mar 14 '19 at 04:05
  • @frazman you need compile java files first. i.e. `javac ./com/example/hello/HelloWorld.java`. – Xin Cheng Oct 27 '21 at 16:50
  • Does anyone knows if this sample works in Databricks Notebooks? I mean, having a JAR File attached to a cluster and call java classes inside that JAR from Scala Notebook. Thanks! – Aldo Saucedo Apr 01 '22 at 20:17
2

The equivalent code would be:

object HelloWorld extends App {
  println("Hello, world!")
}

If you saved that code in a file called HelloWorld.scala then you could compile and run it like so:

$ scalac HelloWorld.scala

$ scala HelloWorld
Hello, world!

Or if you are working in the REPL:

scala> :paste
// Entering paste mode (ctrl-D to finish)

object HelloWorld extends App {
  println("Hello, world!")
}

// Exiting paste mode, now interpreting.

defined module HelloWorld

scala> HelloWorld.main(Array.empty[String])
Hello, world!
sourcedelica
  • 23,940
  • 7
  • 66
  • 74
1
object HelloWorld{
      def main(args: Array[String]): Unit = {
      println("hello world")
    }

}

or

object HelloWorld extends App {
  println("Hello, world!")
}
Rollen Holt
  • 497
  • 2
  • 11
  • 19