Mixing scala and java in Play Framework

后端 未结 4 1031
耶瑟儿~
耶瑟儿~ 2021-01-06 04:53

I have a Java file that looks like this:

package AuthorizeNetFingerprint;


class Fingerprint {
    private static Log logger = LogFactory.getLog(Fingerprint         


        
相关标签:
4条回答
  • 2021-01-06 05:01

    I am not familiar with the Play framework, but the first line in your Scala sample code should instantiate the class AuthorizeNetFingerprint.Fingerprint, which only has a private constructor and is not a public class (i.e., it can only be accessed from the same package).

    Maybe a call to AuthorizeNetFingerprint.Fingerprint.createFingerprint(...) works, after making the class public?

    0 讨论(0)
  • 2021-01-06 05:01

    It's conventional to name Java packages in all lower case.

    It also appears that Scala gets confused if you try to use a Java package that starts with upper case. If you use authorize as the package name instead of AuthorizeNetFingerprint, it will compile.

    Also, there's no need for this:

    val fingerprint = new AuthorizeNetFingerprint.Fingerprint 
    

    createFingerprint is a static method, so just call

    val x_fp_hash = Fingerprint.createFingerprint
    

    (after importing authorize.Fingerprint).

    0 讨论(0)
  • 2021-01-06 05:05

    Three things:

    1. As you've already figured out, your Fingerprint class needs to be public.
    2. You've made Fingerprint's constructor private; you can't instantiate it.
    3. Any static methods in a Java class should be accessed through the class' companion object in Scala.

    All the Scala code in your example should be replaced by:

    val x_fp_hash = AuthorizeNetFingerprint.Fingerprint.createFingerprint(…)
    

    This works in the Scala (2.9.1) console, compiled with sbt (0.11.3).

    Yes, you can mix Java and Scala in a Play2 application, just put the Java code in the app directory. Note that Java classes need to be in their corresponding package directories, which is not the case for Scala classes.

    0 讨论(0)
  • 2021-01-06 05:06

    In theory this should work just fine. Check out this blog Re Java and Scala interop. http://www.codecommit.com/blog/java/interop-between-java-and-scala

    When creating the project did you specify Java, Scala or None (1,2 or 3)

    0 讨论(0)
提交回复
热议问题