cannot call scala from java

41 views
Skip to first unread message

Mohit Jaggi

unread,
Nov 15, 2012, 8:08:16 PM11/15/12
to scala...@googlegroups.com
Folks,
I am trying to call a Scala method from from Java code. My scala code looks like this:

class Demo {

}

object Demo {

def userListener(userName: String, userMac: MACAddress, bytes: Double) = {

  println("userListener: " + userName + userMac + bytes)

}

}


And calling Java code looks like this: 

public class UserSubscriber {

public void update(String u, MACAddress m, double b) {

System.out.println("UserSubscriber: Received: user= " + u + " mac=" + m + " bytes=" + b);

Demo.userListener(u, m, b); 

}

I don't get any compilation failures. I had started this as a Java project in Eclipse (scala version from coursera scala class) and then added "scala nature" to the project before creating Demo scala file.

I don't see any runtime errors but Demo.userListener is not called. In debugger, I see an exception being thrown for class not found and class name is not Demo but scala/Object. Any help is appreciated.

}

Mohit Jaggi

unread,
Nov 15, 2012, 8:14:57 PM11/15/12
to scala...@googlegroups.com
sorry the exception is 

java.lang.NoClassDefFoundError: scala/ScalaObject


Nils Kilden-Pedersen

unread,
Nov 15, 2012, 8:24:19 PM11/15/12
to Mohit Jaggi, scala...@googlegroups.com
You need to add the scala-library.jar to your classpath.

Mohit Jaggi

unread,
Nov 15, 2012, 8:42:16 PM11/15/12
to Nils Kilden-Pedersen, scala...@googlegroups.com
Nils,
Thanks for your response. 
That happened when I added "scala nature" to the project. In project->properties->libraries, I see scala with three jar files including scala-library.jar

Mohit.

Vlad Patryshev

unread,
Nov 15, 2012, 9:01:41 PM11/15/12
to Mohit Jaggi, scala...@googlegroups.com
I have to confess, specifically for java interop I (use to) create the following structure:

trait Demo {
  // contains all your functionality; materialize it using cake pattern
}

object Demo extends Demo {
  // all the stuff you need
}

class DemoOps extends Demo {} // this is what Java always sees/uses; can be caked if needed.

Thanks,
-Vlad


On Thu, Nov 15, 2012 at 5:08 PM, Mohit Jaggi <mohit...@gmail.com> wrote:

Mohit Jaggi

unread,
Nov 16, 2012, 2:50:49 PM11/16/12
to Vlad Patryshev, scala...@googlegroups.com
Do I really need all that? I went through the Scala book by Martin Odersky and according to that, if I don't have the userListener method in the class, have it in the object only, I should be able to call it directly.
If I try to add a $ to Demo in the call, compilation fails...so I think the call and its compilation are fine. For some reason, scala library although added to the project is not being "linked".

Mohit Jaggi

unread,
Nov 16, 2012, 4:25:56 PM11/16/12
to Vlad Patryshev, scala...@googlegroups.com
I tried using command line and the program works...so something is wrong in Eclipse. Although, it has Scala library in build properties, it doesn't find it at runtime. I removed the scala library, compilation failed and Eclipse asked me if I wanted to add Scala to project. I chose yes but still have the same issue.
Reply all
Reply to author
Forward
0 new messages