将 .jar 添加到类路径 (Scala)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10077899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:05:15  来源:igfitidea点击:

Adding .jar's to classpath (Scala)

scalajarjvmclasspath

提问by adelbertc

So I've been trying to work with the signal-collectframework and I downloaded the .jarfiles and extracted it into a folder. Currently the folder structure looks like:

所以我一直在尝试使用信号收集框架,我下载了.jar文件并将其解压缩到一个文件夹中。目前文件夹结构如下所示:

LICENSE.txt  
PageRank.scala  
core-1.1.1-sources.jar  
dependencies/  
javaapi-1.1.1-sources.jar  
NOTICE.txt  
README.txt  
core-1.1.1.jar  
javaapi-1.1.1-javadoc.jar  
javaapi-1.1.1.jar  

Where PageRank.scalais the Scala test code they provide, which is:

PageRank.scala他们提供的 Scala 测试代码在哪里,即:

import com.signalcollect._

object PageRank extends App {
  val graph = GraphBuilder.build
  graph.addVertex(new PageRankVertex(id=1))
  graph.addVertex(new PageRankVertex(id=2))
  graph.addEdge(new PageRankEdge(sourceId=1, targetId=2))
  graph.addEdge(new PageRankEdge(sourceId=2, targetId=1))
  graph.execute
  graph.foreachVertex(println(_))
  graph.shutdown
}

class PageRankVertex(id: Any, dampingFactor: Double=0.85)
    extends DataGraphVertex(id=id, state=1-dampingFactor) {
  type Signal = Double

  def collect(oldState: Double, mostRecentSignals: Iterable[Double]): Double = {
    1 - dampingFactor + dampingFactor * mostRecentSignals.sum
  }

}

class PageRankEdge(sourceId: Any, targetId: Any)
    extends DefaultEdge(sourceId, targetId) {
  type SourceVertex = PageRankVertex

  def signal(sourceVertex: PageRankVertex) = {
    sourceVertex.state * weight / sourceVertex.sumOfOutWeights
  }

}

I am a newbie when it comes to the JVM/Java/Scala, and this was my attempt at adding the .jar'sto the classpath for compiling PageRank.scala:

我是 JVM/Java/Scala 的新手,这是我尝试将 加入.jar's到类路径以进行编译PageRank.scala

$ scalac -classpath *.jar dependencies/*.jar PageRank.scala 
error: IO error while decoding core-1.1.1.jar with UTF-8
Please try specifying another one using the -encoding option
error: IO error while decoding javaapi-1.1.1-javadoc.jar with UTF-8
Please try specifying another one using the -encoding option
error: IO error while decoding javaapi-1.1.1-sources.jar with UTF-8
Please try specifying another one using the -encoding option
error: IO error while decoding javaapi-1.1.1.jar with UTF-8
Please try specifying another one using the -encoding option
error: IO error while decoding dependencies/je-3.2.76.jar with UTF-8
Please try specifying another one using the -encoding option
error: IO error while decoding dependencies/scala-library-2.9.1.jar with UTF-8
Please try specifying another one using the -encoding option
6 errors found

I cannot figure out what is going wrong... what's happening? Thanks! Regards, -kstruct

我不知道出了什么问题......发生了什么?谢谢!问候,-kstruct

回答by dhg

You need to pass both classpath paths as a single argument.

您需要将两个类路径路径作为单个参数传递。

Try this:

试试这个:

$ scalac -classpath "*.jar:dependencies/*.jar" PageRank.scala
$ scala -classpath "*.jar:dependencies/*.jar" PageRank
PageRankVertex(id=2, state=0.9999999999999997)
PageRankVertex(id=1, state=0.9999999999999997)

It worked for me.

它对我有用。

回答by Maksym

It seems that depending on an installed version of Java, wildcards in classpath to include multiple JARs might or might not work. I found this trickelsewhere on StackOverflow (note that you can have as many folders after the 'echo' as you wish, separated by spaces):

似乎根据已安装的 Java 版本,类路径中包含多个 JAR 的通配符可能有效,也可能无效。我在 StackOverflow 的其他地方发现了这个技巧(请注意,您可以根据需要在“回声”之后拥有任意数量的文件夹,以空格分隔):

scalac -classpath $(echo *.jar dependencies/*.jar | tr ' ' ':')  PageRank.scala
scala -classpath $(echo *.jar dependencies/*.jar | tr ' ' ':')  PageRank

回答by Maksym

for simplicity ,you can just use: scala -classpath $(echo *.jar dependencies/*.jar | tr ' ' ':') PageRank.scala

为简单起见,您可以使用: scala -classpath $(echo *.jar dependencies/*.jar | tr ' ' ':') PageRank.scala