英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论