0

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>
hba
  • 7,406
  • 10
  • 63
  • 105

2 Answers2

0

There are a few things you should check.

First, you must ensure you have a scala sdk present in the project. If you're using IntelliJ IDEA, as soon as you go to the scala file it will warn you.

The .iml should have an entry like this:

< orderEntry type="library" name="scala-sdk-2.11.7" level="application" />

If you use maven, you should add a entry like this:

< dependency>
    < artifactId>scala-library</artifactId>
    < groupId>org.scala-lang</groupId>
    < version>${scala.version}</version>
</dependency>

Once you make sure that you have the scala sdk, you should import the scala object before using it:

pkg1

package pkg1

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

Also, don't forget that the main method should instantiate the class with new JHelloWorld()

pkg2

package pkg2;

import pkg1.HelloWorld$;

public class JHelloWorld {
    HelloWorld$ scalaObject = HelloWorld$.MODULE$;

    public static void main(String argv[]) {
        new JHelloWorld().scalaObject.main(argv);
    }
}

The above should work fine

Connected to the target VM, address: '127.0.0.1:46179', transport: 'socket'
Disconnected from the target VM, address: '127.0.0.1:46179', transport: 'socket'
Hello, world!

Process finished with exit code 0
brunofitas
  • 2,983
  • 1
  • 20
  • 26
  • As long as I don't reference the scala code in the java code my project builds fine. And when I inspect the jar file, I see both the scala class and the java class. But for some reason I cannot import the class into my java class – hba Feb 06 '17 at 17:41
0

The important thing is that the class files produced by the Scala compiler are available in the classpath when you run the Java compiler.

If you are using SBT, the directory structure comprising the scala directory is already part of the structure expected by the build tool. If you are using another build tool, like Maven or Gradle, you may want to install the relevant plugin (here for Maven, here for Gradle).

If you are not using a build tool, you'll have to adjust the class path manually.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63