Scala - Tips

1- 运行scala命令,提示报错

问题现象:
在Windows7系统中安装scala后(直接安装MSI包,或者解压zip包添加环境变量的方式),执行scala命令报错,但可以执行scala -version
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\guowli>scala
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help. scala> java.lang.NullPointerException
at java.util.Arrays.sort(Arrays.java:1438)
at scala.tools.nsc.classpath.JFileDirectoryLookup.listChildren(DirectoryClassPath.scala:113)
at scala.tools.nsc.classpath.JFileDirectoryLookup.listChildren$(DirectoryClassPath.scala:97)
at scala.tools.nsc.classpath.DirectoryClassPath.listChildren(DirectoryClassPath.scala:202)
at scala.tools.nsc.classpath.DirectoryClassPath.listChildren(DirectoryClassPath.scala:202)
at scala.tools.nsc.classpath.DirectoryLookup.list(DirectoryClassPath.scala:73)
at scala.tools.nsc.classpath.DirectoryLookup.list$(DirectoryClassPath.scala:69)
at scala.tools.nsc.classpath.DirectoryClassPath.list(DirectoryClassPath.scala:202)
at scala.tools.nsc.classpath.AggregateClassPath.$anonfun$list$1(AggregateClassPath.scala:76)
at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:234)
at scala.collection.Iterator.foreach(Iterator.scala:929)
at scala.collection.Iterator.foreach$(Iterator.scala:929)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
at scala.collection.IterableLike.foreach(IterableLike.scala:71)
at scala.collection.IterableLike.foreach$(IterableLike.scala:70)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at scala.collection.TraversableLike.map(TraversableLike.scala:234)
at scala.collection.TraversableLike.map$(TraversableLike.scala:227)
at scala.collection.AbstractTraversable.map(Traversable.scala:104)
at scala.tools.nsc.classpath.AggregateClassPath.list(AggregateClassPath.scala:74)
at scala.tools.nsc.symtab.SymbolLoaders$PackageLoader.doComplete(SymbolLoaders.scala:271)
at scala.tools.nsc.symtab.SymbolLoaders$SymbolLoader.complete(SymbolLoaders.scala:220)
at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:1530)
at scala.reflect.internal.Mirrors$RootsBase.init(Mirrors.scala:225)
at scala.tools.nsc.Global.rootMirror$lzycompute(Global.scala:65)
at scala.tools.nsc.Global.rootMirror(Global.scala:63)
at scala.tools.nsc.Global.rootMirror(Global.scala:36)
at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass$lzycompute(Definitions.scala:267)
at scala.reflect.internal.Definitions$DefinitionsClass.ObjectClass(Definitions.scala:267)
at scala.reflect.internal.Definitions$DefinitionsClass.init(Definitions.scala:1422)
at scala.tools.nsc.Global$Run.<init>(Global.scala:1164)
at scala.tools.nsc.interpreter.IMain._initialize(IMain.scala:125)
at scala.tools.nsc.interpreter.IMain.initializeSynchronous(IMain.scala:147)
at scala.tools.nsc.interpreter.ILoop.$anonfun$process$11(ILoop.scala:1030)
at scala.tools.nsc.interpreter.ILoop.startup$1(ILoop.scala:1011)
at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:1049)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:82)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:85)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:96)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:101)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) C:\Users\guowli> C:\Users\guowli>scala -version
Scala code runner version 2.12.4 -- Copyright 2002-2017, LAMP/EPFL and Lightbend, Inc. C:\Users\guowli>
原因分析:
可以执行scala -version命令,说明scala的环境变量是正确的。
无法scala命令,可能是调用相关库失败,检查Java是否正确安装和Scala环境变量Classpath的配置
处理方法:
1- 检查java和javac的版本是否可用并一致
2- 检查环境变量
设置 Classpath 变量(也可能为CLASSPATH,不区分大小写):找到找到系统变量下的"Classpath",单击编辑,添加如下内容
;%SCALA_HOME%\bin;%SCALA_HOME%\lib\dt.jar;%SCALA_HOME%\lib\tools.jar.;

注意:"变量值"最前面的 .; 不要漏掉。最后单击确定即可。

2- 在Win7 64bit系统cmd命令行下运行sbt命令,提示报错

在Windows7 64bit系统中安装sbt(未重启系统)后,在cmd命令行下执行sbt命令报错
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\guowli>sbt
"C:\Users\guowli\.sbt\preloaded\org.scala-sbt\sbt\"1.0.2"\jars\sbt.jar"
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
Getting org.fusesource.jansi jansi 1.11 ... :: problems summary ::
:::: WARNINGS
module not found: org.fusesource.jansi#jansi;1.11 ==== local: tried C:\Users\guowli\.ivy2\local\org.fusesource.jansi\jansi\1.11\ivys\ivy.xml -- artifact org.fusesource.jansi#jansi;1.11!jansi.jar: C:\Users\guowli\.ivy2\local\org.fusesource.jansi\jansi\1.11\jars\jansi.jar ==== local-preloaded-ivy: tried file:/C:/Users/guowli/.sbt/preloaded/org.fusesource.jansi/jansi/1.11/ivys/ivy.xml ==== local-preloaded: tried file:/C:/Users/guowli/.sbt/preloaded/org/fusesource/jansi/jansi/1.11/jansi-1.11.pom -- artifact org.fusesource.jansi#jansi;1.11!jansi.jar: file:/C:/Users/guowli/.sbt/preloaded/org/fusesource/jansi/jansi/1.11/jansi-1.11.jar ==== Maven Central: tried https://repo1.maven.org/maven2/org/fusesource/jansi/jansi/1.11/jansi-1.11.pom -- artifact org.fusesource.jansi#jansi;1.11!jansi.jar: https://repo1.maven.org/maven2/org/fusesource/jansi/jansi/1.11/jansi-1.11.jar ==== sbt-maven-releases: tried https://repo.scala-sbt.org/scalasbt/maven-releases/org/fusesource/jansi/jansi/1.11/jansi-1.11.pom -- artifact org.fusesource.jansi#jansi;1.11!jansi.jar: https://repo.scala-sbt.org/scalasbt/maven-releases/org/fusesource/jansi/jansi/1.11/jansi-1.11.jar ==== sbt-maven-snapshots: tried https://repo.scala-sbt.org/scalasbt/maven-snapshots/org/fusesource/jansi/jansi/1.11/jansi-1.11.pom -- artifact org.fusesource.jansi#jansi;1.11!jansi.jar: https://repo.scala-sbt.org/scalasbt/maven-snapshots/org/fusesource/jansi/jansi/1.11/jansi-1.11.jar ==== typesafe-ivy-releases: tried https://repo.typesafe.com/typesafe/ivy-releases/org.fusesource.jansi/jansi/1.11/ivys/ivy.xml ==== sbt-ivy-snapshots: tried https://repo.scala-sbt.org/scalasbt/ivy-snapshots/org.fusesource.jansi/jansi/1.11/ivys/ivy.xml :::::::::::::::::::::::::::::::::::::::::::::: :: UNRESOLVED DEPENDENCIES :: :::::::::::::::::::::::::::::::::::::::::::::: :: org.fusesource.jansi#jansi;1.11: not found :::::::::::::::::::::::::::::::::::::::::::::: :::: ERRORS
Server access Error: Connection timed out: connect url=https://repo1.maven.org/maven2/org/fusesource/jansi/jansi
/1.11/jansi-1.11.pom Server access Error: Connection timed out: connect url=https://repo1.maven.org/maven2/org/fusesource/jansi/jansi
/1.11/jansi-1.11.jar Server access Error: Connection timed out: connect url=https://repo.scala-sbt.org/scalasbt/maven-releases/org/fu
sesource/jansi/jansi/1.11/jansi-1.11.pom Server access Error: Connection timed out: connect url=https://repo.scala-sbt.org/scalasbt/maven-releases/org/fu
sesource/jansi/jansi/1.11/jansi-1.11.jar Server access Error: Connection timed out: connect url=https://repo.scala-sbt.org/scalasbt/maven-snapshots/org/f
usesource/jansi/jansi/1.11/jansi-1.11.pom Server access Error: Connection timed out: connect url=https://repo.scala-sbt.org/scalasbt/maven-snapshots/org/f
usesource/jansi/jansi/1.11/jansi-1.11.jar Server access Error: Connection timed out: connect url=https://repo.typesafe.com/typesafe/ivy-releases/org.fuses
ource.jansi/jansi/1.11/ivys/ivy.xml Server access Error: Connection timed out: connect url=https://repo.scala-sbt.org/scalasbt/ivy-snapshots/org.fus
esource.jansi/jansi/1.11/ivys/ivy.xml :: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS
unresolved dependency: org.fusesource.jansi#jansi;1.11: not found
Error during sbt execution: Error retrieving required libraries
(see C:\Users\guowli\.sbt\boot\update.log for complete log)
Error: Could not retrieve jansi 1.11 C:\Users\guowli>
原因分析:
网络原因的导致无法获得依赖包
处理方法:
  • 设置可用的repo或代理。
  • 在用户目录下创建.sbt目录,放一个repositories文件
  • 在用户的~/.sbt目录和安装目录,例如:“C:\Program Files (x86)\sbt\conf”
guowli@5CG450158J MINGW64 ~/.sbt
$ ll
total 17
drwxr-xr-x 1 guowli 1049089 0 Nov 2 11:29 1.0/
drwxr-xr-x 1 guowli 1049089 0 Nov 2 13:38 boot/
drwxr-xr-x 1 guowli 1049089 0 Oct 26 10:55 preloaded/
-rw-r--r-- 1 guowli 1049089 880 Nov 2 13:29 repositories
-rw-r--r-- 1 guowli 1049089 253 Nov 2 13:30 sbtconfig.txt guowli@5CG450158J MINGW64 ~/.sbt
$ cat repositories
[repositories]
local
maven-repo2-ivy-releases: http://repo2.maven.org/maven2/, , [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
maven-repo2: http://repo2.maven.org/maven2/
maven-central-ivy-releases: http://repo1.maven.org/maven2/, , [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
maven-central: http://repo1.maven.org/maven2/
sbt-releases-repo: http://repo.typesafe.com/typesafe/ivy-releases/, , [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
sbt-plugins-repo: http://repo.scala-sbt.org/scalasbt/sbt-plugin-releases/, , [organization]/[module]/(scala_[scalaVersion]/)(sbt_[sbtVersion]/)[revision]/[type]s/[artifact](-[classifier]).[ext]
guowli@5CG450158J MINGW64 ~/.sbt
$ guowli@5CG450158J MINGW64 ~/.sbt
$ cat sbtconfig.txt
# Set the java args to high -Xmx512M -XX:MaxPermSize=256m -XX:ReservedCodeCacheSize=128m # Set the extra SBT options -Dsbt.log.format=true -Dhttp.proxySet=true -Dhttp.proxyHost=10.144.1.10 -Dhttp.proxyPort=8080 -Dsbt.override.build.repos=true
guowli@5CG450158J MINGW64 ~/.sbt
$

参考信息:

3- 在REPL命令行下,编辑大块代码

示例: 利用“:paste”选项
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\guowli>scala
Welcome to Scala 2.12.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_101).
Type in expressions for evaluation. Or try :help. scala> :paste
// Entering paste mode (ctrl-D to finish) def qSort(x: List[Int]): List[Int] = {
if (x.length <= 2) x
else qSort(x.filter(x.head > _)) ++
x.filter(x.head == _) ++
qSort(x.filter(x.head < _))
} // Exiting paste mode, now interpreting. qSort: (x: List[Int])List[Int] scala> qSort(List(6, 4, 5, 3, 1, 2, 9, 8, 0, 7))
res0: List[Int] = List(0, 1, 2, 3, 4, 5, 6, 8, 7, 9) scala>

4- main方法

对于一个Scala应用程序而言,必须包含main方法。
如果没有包含main方法,就不能使用scalac命令进行编译,而是直接使用scala命令运行代码。
 
 
上一篇:CSS3 transition 属性 过渡效果


下一篇:apache开源项目--ApacheDS