Scala的JAR包:错误:无法找到或加载主类

huangapple go评论81阅读模式
英文:

Scala jar: Error: Could not find or load main class

问题

以下是翻译好的内容:

我正在尝试运行一个使用sbt clean compile package创建的Scala应用程序的jar包,操作如下:

java -cp /scala-hello-world.jar:/scala-library-2.12.2.jar HelloWorld

这是目录结构:

.
├── HelloWorld.class
├── HelloWorld$.class
├── scala-hello-world.jar
└── scala-library-2.12.2.jar

但是当我尝试执行时,我收到以下错误:

错误:找不到或无法加载主类HelloWorld
原因:java.lang.ClassNotFoundException: HelloWorld

起初,我以为是因为目录中缺少类文件,但是即使我已经将它们重新添加进去,我仍然收到相同的错误。我还尝试了:

java -cp . HelloWorld

有什么想法吗?

以下是HelloWorld.scala的内容:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}
英文:

I am trying to run a jar for a Scala application (created using sbt clean compile package) as follows:

java -cp /scala-hello-world.jar:/scala-library-2.12.2.jar HelloWorld

Here is the directory structure:

.
├── HelloWorld.class
├── HelloWorld$.class
├── scala-hello-world.jar
└── scala-library-2.12.2.jar

But when I try to execute it, I receive the error:

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld

At first, I thought it was because my directory was missing the class files, but since I've added them back in, I still receive the same error. I've also tried:

java -cp . HelloWorld

Any ideas?

These are the contents of HelloWorld.scala:

object HelloWorld {
  def main(args: Array[String]): Unit = {
    println("Hello, world!")
  }
}

答案1

得分: 1

我认为你的类路径有一个错误。如果你在当前目录中,其中包含

.
├── scala-hello-world.jar
└── scala-library-2.12.2.jar

那么请尝试使用以下命令来执行:

java -cp scala-library-2.12.2.jar:scala-hello-world.jar HelloWorld

而不是

java -cp /scala-library-2.12.2.jar:/scala-hello-world.jar HelloWorld
英文:

I believe your classpath has a bug. If you are in the current directory which has

.
├── scala-hello-world.jar
└── scala-library-2.12.2.jar

then the try executing with

java -cp scala-library-2.12.2.jar:scala-hello-world.jar HelloWorld

instead of

java -cp /scala-library-2.12.2.jar:/scala-hello-world.jar HelloWorld

huangapple
  • 本文由 发表于 2020年4月6日 05:11:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/61049552.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定