31

Here is a simple test I'm using to invoke a Scala method from Java:

public static void main(String args[]) {
  java.util.Map<String, java.util.List<String>> rec = news.recommend.DriverObj.runTest();     
  System.out.println(rec.toString());
}

Here is the definition of the Scala method:

def runTest: java.util.Map[String, java.util.List[String]] = {
  new java.util.HashMap[String, java.util.List[String]]
}

But it throws an error:

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Function1
    at news.recommend.DriverObj.runTest(DriverObj.scala)

What should I do to make this running smoothly?

Update : I'm running it via Eclipse and my build path contains :

enter image description here

So Scala library should be found ?

Riduidel
  • 22,052
  • 14
  • 85
  • 185
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • What's on your class path when you run it? – Dave Newton Jun 25 '14 at 18:16
  • 3
    It looks to me like you aren't including the jar for the scala library in the classpath. `scala` includes it automatically, `java` does not. – wingedsubmariner Jun 25 '14 at 18:16
  • @Dave Newton please see question update – blue-sky Jun 25 '14 at 18:21
  • 1
    Build path and run path aren't necessarily the same thing, I too believe the Scala lib isn't on the runtime classpath. – Dave Newton Jun 25 '14 at 18:33
  • @Dave Newton ive udpated question again. so Maven adds dependencies to the run path while Eclipse does not ? – blue-sky Jun 25 '14 at 18:38
  • How are you running the code? If you are running it from Eclipse, look in the run configuration that you are using to run it. Eclipse's Maven plugin probably automatically puts it in the run config for you. – Jesper Jun 25 '14 at 20:17
  • @Jesper im running from Eclipse, and yes when I add the Scala Maven dependency it runs correctly – blue-sky Jun 26 '14 at 22:48
  • Where did you add the scala dependency? Did you add it as text? I have exactly the same problem! – StackG Jun 29 '15 at 08:44
  • @StackG I added it to project pom file. Yes I added it as text, although Im not sure how else it could be added. – blue-sky Jun 29 '15 at 09:03
  • I'm using Eclipse *without* Maven at the moment and don't have a .pom file, how do I convert it / add a .pom file? Many thanks! – StackG Jun 29 '15 at 09:16
  • 1
    @StackG, you can manually add `scala-library.jar` as a library to your Eclipse project, the way you add any normal jar when using Java. – Jus12 Oct 05 '15 at 04:33
  • @blue-sky I moved your answer into an answer. I made it a community wiki to not gain reputation based upon your work. However, can you accept the answer ? – Riduidel Dec 03 '15 at 16:11

2 Answers2

24

adding the Scala dependency to the maven build can fix the issue :

<dependency>
    <groupId>org.scala-lang</groupId>
    <artifactId>scala-library</artifactId>
    <version>2.10.3</version>
</dependency>
Riduidel
  • 22,052
  • 14
  • 85
  • 185
21

As for me, in the configuration of "Run/Debug configurations" [idealJ]

remember to choose the "Include dependencies with "Provided" scope"

Kuang Wenyi
  • 311
  • 2
  • 8