从零开始学Scala(一)——Scala环境搭建与第一行代码

来源:互联网 发布:c语言开发环境 编辑:程序博客网 时间:2024/06/06 02:58

开发环境搭建

  1. 安装JDK
  2. 安装Scala, scala下载页面 下载版本2.12.3
  3. 安装Intellij idea插件, Intellij idea Scala插件下载

环境搭建

1、Scala SDK安装与Idea插件安装,注意Idea插件与版本要匹配,前提是要装好JDK,参考百度上的帖子很容易

在Intellij idea中用Maven+plugin搭建Scala项目

参考帖子

  1. Idea中新建项目scala-study
  2. Maven构建,pom.xml如下
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.shannonAJ</groupId>    <artifactId>scala-study</artifactId>    <version>1.0-SNAPSHOT</version>    <dependencies>        <dependency>            <groupId>org.scala-lang</groupId>            <artifactId>scala-library</artifactId>            <version>2.12.3</version>        </dependency>        <dependency>            <groupId>org.scala-lang</groupId>            <artifactId>scala-compiler</artifactId>            <version>2.12.3</version>        </dependency>        <dependency>            <groupId>org.scala-lang</groupId>            <artifactId>scala-reflect</artifactId>            <version>2.12.3</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-api</artifactId>            <version>1.7.21</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.scala-tools</groupId>                <artifactId>maven-scala-plugin</artifactId>                <version>2.15.1</version>                <executions>                    <execution>                        <goals>                            <goal>compile</goal>                            <goal>testCompile</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build></project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  1. main下添加Java package和 scala package
  2. HelloScala.Scala
package com.shannonAJ.scala/**  * Created by ShannonAJ on 2017/8/13.  */class HelloScala {  def sayHello(x:String): Unit = {      print("Hello " + x);  }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

5.HelloDemo.java

package com.shannonAJ.task;import com.shannonAJ.scala.HelloScala;/** * Created by ShannonAJ on 2017/8/13. */public class HelloDemo {    public static void main(String[] args) {        System.out.println("use scala class");        HelloScala hello = new HelloScala();        hello.sayHello("scala");    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  1. 编译
mvn clean scala:compile compile
  • 1
  1. 运行结果
use scala classHello scalaProcess finished with exit code 0
  • 1
  • 2
  • 3
  • 4
原创粉丝点击