简体   繁体   中英

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:

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!")
  }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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