Search code examples
javascalaimplicits

Integrating scala with java


I'm new to Scala. I need help in resolving this issue.
I have built a project which is written in Scala and I'm trying to integrate it with a Java project. I have taken the dependency on the build scala jar in my Java project in eclipse. However when I call a method in the Scala jar I get ClassCastException when running the application:

Below is the exception I get

Exception in thread "main" java.lang.ClassCastException: com.rockymadden.stringmetric.phonetic.MetaphoneAlgorithm cannot be cast to com.rockymadden.stringmetric.StringFilter at com.rockymadden.stringmetric.phonetic.MetaphoneAlgorithm.compute(MetaphoneAlgorithm.scala:10) at org.gobe.search.dictionary.test.PhoneticTest.main(PhoneticTest.java:16)

What can I do to fix this ?

public static void main(String[] args) {
    MetaphoneMetric metric = new MetaphoneMetric();
    DummyImplicit di = new DummyImplicit();
    MetaphoneAlgorithm algo = new MetaphoneAlgorithm();
    char[] rr = {'h', 'e', 'l', 'l', 'o'};
    System.out.println(algo.compute(rr, di));
}

In the above code "MetaphoneMetric" , "DummyImplicit" are part of Scala Jar I generated.
Logic of Metaphone algorithm can be found at http://pastebin.com/d7CXjDtx


Solution

  • Please check this:

    Predef.DummyImplicit di = new Predef.DummyImplicit();
    MetaphoneAlgorithm algo = MetaphoneAlgorithm.apply();
    char[] rr = {'h', 'e', 'l', 'l', 'o'};
    System.out.println(Arrays.toString(algo.compute(rr, di).get()));