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

开发环境搭建

  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. 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);
  }

}

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. 编译
mvn clean scala:compile compile
  1. 运行结果
use scala class
Hello scala
Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值