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

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

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

问题

以下是翻译好的内容:

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

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

这是目录结构:

  1. .
  2. ├── HelloWorld.class
  3. ├── HelloWorld$.class
  4. ├── scala-hello-world.jar
  5. └── scala-library-2.12.2.jar

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

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

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

  1. java -cp . HelloWorld

有什么想法吗?

以下是HelloWorld.scala的内容:

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

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

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

Here is the directory structure:

  1. .
  2. ├── HelloWorld.class
  3. ├── HelloWorld$.class
  4. ├── scala-hello-world.jar
  5. └── scala-library-2.12.2.jar

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

  1. Error: Could not find or load main class HelloWorld
  2. 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:

  1. java -cp . HelloWorld

Any ideas?

These are the contents of HelloWorld.scala:

  1. object HelloWorld {
  2. def main(args: Array[String]): Unit = {
  3. println("Hello, world!")
  4. }
  5. }

答案1

得分: 1

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

  1. .
  2. ├── scala-hello-world.jar
  3. └── scala-library-2.12.2.jar

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

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

而不是

  1. 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

  1. .
  2. ├── scala-hello-world.jar
  3. └── scala-library-2.12.2.jar

then the try executing with

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

instead of

  1. 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:

确定