Why do I get `java.lang.NoClassDefFoundError: scala/Function1` when I run my code in ScalaIDE?
Asked Answered
G

2

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 ?

Groce answered 25/6, 2014 at 18:14 Comment(12)
What's on your class path when you run it?Tubman
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.Ethel
@Dave Newton please see question updateGroce
Build path and run path aren't necessarily the same thing, I too believe the Scala lib isn't on the runtime classpath.Tubman
@Dave Newton ive udpated question again. so Maven adds dependencies to the run path while Eclipse does not ?Groce
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.Windjammer
@Windjammer im running from Eclipse, and yes when I add the Scala Maven dependency it runs correctlyGroce
Where did you add the scala dependency? Did you add it as text? I have exactly the same problem!Prudhoe
@Prudhoe I added it to project pom file. Yes I added it as text, although Im not sure how else it could be added.Groce
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!Prudhoe
@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.Blockade
@Groce 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 ?Wits
W
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>
Wits answered 25/6, 2014 at 18:14 Comment(2)
Didn't help for me. I am running through Gatling. Looks like Gatling controls its classpath differently.Florey
I already had this dependency listed and was still having the issue. The solution from @Kuang Wenyi worked for me.Sanjiv
B
21

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

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

Becoming answered 21/2, 2019 at 14:21 Comment(3)
Worked for me. Thanks!Sanjiv
Worked for me also, but why?Insignificancy
@Insignificancy I believe there is some issue between the IdeaJ and maven.Becoming

© 2022 - 2024 — McMap. All rights reserved.