I have a project:
- src/main/java/pkg2/JHelloWorld.java
- src/main/scala/pkg1/HelloWorld.scala
Let's say HelloWorld.scala is like:
package pkg2
object HelloWorld extends App {
println("Hello, world!")
}
How do I invoke the HelloWorld from JHelloWorld:
package pkg1;
public class JHelloWorld {
public static void main(String[] args) {
//pkg2.HelloWorld.main(args);
}
}
The compiler complains that pkg2 doesn't exist...
Do I need to put the scala code in a different module (jar file) and go from there?
Thanks for the input but at the end this tutorial really helped.
Although I was using the maven-scala-plugin
I had two problems:
- I was not setting the source directory for scala
- I was not building the scala code first
Now my pom looks like this:
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<executions>
<!-- Run scala compiler in the process-resources phase, so that dependencies
on scala classes can be resolved later in the (Java) compile phase -->
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<!-- Run scala compiler in the process-test-resources phase, so that
dependencies on scala classes can be resolved later in the (Java) test-compile
phase -->
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>