简体   繁体   English

Scala jar:错误:无法找到或加载主 class

[英]Scala jar: Error: Could not find or load main class

I am trying to run a jar for a Scala application (created using sbt clean compile package ) as follows:我正在尝试为 Scala 应用程序运行 jar(使用sbt clean compile package创建如下)

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.起初,我以为是因为我的目录缺少 class 文件,但由于我已将它们重新添加,我仍然收到相同的错误。 I've also tried:我也试过:

java -cp . HelloWorld

Any ideas?有任何想法吗?

These are the contents of HelloWorld.scala:以下是 HelloWorld.scala 的内容:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM