2

I would like to call the following Scala code in Java:

Scala code

package calculate

import java.io._

class CalculationScala

object CalculationScala {
  def main(args: Array[String]) {
    def operate(a: Double, b: Double, op: (Double, Double) => Double): Double = (op(a, b))
    println(operate(5, 15, _-_))
  }
}

Java code

package calculate;

public class Calculation {
    public static void main(String[] args) {
        CalculationScala calculationScala = new CalculationScala();
        calculationScala.main(args);
    }
}

but the following error occurs.

Error

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function2
    at calculate.CalculationScala.main(CalculationScala.scala)
    at calculate.Calculation.main(Calculation.java:79)
Caused by: java.lang.ClassNotFoundException: scala.Function2
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 2 more

How to solve this issue?

1 Answer 1

2

It looks like you are missing scala-library.jar on your runtime classpath.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the answer. I have downloaded Scala.msi, installed it and added the scala-library.jar located at <install_directory_scala>/lib to the libraries java build path of the Java Project by adding it as an External JAR (right click java project, Java Build Path, Libraries tab, Add External JARs...).
That is correct. I have accepted and upvoted the answer. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.