Java&Scala混合编程

1. mvn 创建一个普通的java工程后,添加如下信息:



    4.0.0

    org.jerry
    mix-test
    1.0-SNAPSHOT

    
        2.4.2
        2.11.1
        1.7.7
        2.11.1
        UTF-8
    

    
        package
        
            
                src/main/resources
                true
            
            
                src/test/resources
                true
            
        
        
            
                org.apache.maven.plugins
                maven-resources-plugin
                
                    ${project.build.sourceEncoding}
                
                
                    
                        
                            copy-resources
                        
                    
                
            
            
                net.alchim31.maven
                scala-maven-plugin
                3.2.0
                
                    incremental
                    
                        -target:jvm-1.7
                    
                    
                        -source
                        1.7
                        -target
                        1.7
                    
                
                
                    
                        scala-compile
                        process-resources
                        
                            compile
                        
                    
                    
                        scala-test-compile
                        process-test-resources
                        
                            testCompile
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.7
                    1.7
                
                
                    
                        compile
                        
                            compile
                        
                    
                
            

        
        
            
                
                
                    org.eclipse.m2e
                    lifecycle-mapping
                    1.0.0
                    
                        
                            
                                
                                    
                                        
                                            net.alchim31.maven
                                        
                                        
                                            scala-maven-plugin
                                        
                                        
                                            [3.1.6,)
                                        
                                        
                                            compile
                                            testCompile
                                        
                                    
                                    
                                        
                                    
                                
                            
                        
                    
                
            
        
    


2. 在src/main/下,创建 scala 目录

                   src/main/java

                   src/main/scala

3. demo:

Java class:

//package mix.java.scala;
 
public class GreetingInJava {
    public void greet() {
        System.out.println("Hello World!");
    }
}

Scala class:

class GreetingInScala {
  def greet: Unit = {
    val delegate: GreetingInJava = new GreetingInJava
    delegate.greet
  }
}


Java class:

public class Bootstrap {
 
    public static void main(String[] args) {
        GreetingInScala scala = new GreetingInScala();
        scala.greet();
    }
}



你可能感兴趣的:(Java&Scala混合编程)