gpt4 book ai didi

java - 为什么 SBT 检测不到 Java 中的主类? (未检测到主类)

转载 作者:行者123 更新时间:2023-11-29 05:37:30 25 4
gpt4 key购买 nike

SBT 会编译,但不会运行这个非常简单的项目。为什么不呢?

mackler@localhost:~/tmp/javahello$ sbt --version                                                                       
sbt launcher version 0.12.1
mackler@localhost:~/tmp/javahello$ find .
.
./src
./src/main
./src/main/java
./src/main/java/learn
./src/main/java/learn/HelloWorld.java
mackler@localhost:~/tmp/javahello$ cat src/main/java/learn/HelloWorld.java
package learn;

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
mackler@localhost:~/tmp/learn/javahello$ sbt run
[info] Set current project to default-1004d5 (in build file:/home/mackler/tmp/javahello/)
[info] Updating {file:/home/mackler/tmp/javahello/}default-1004d5...
[info] Resolving org.scala-lang#scala-library;2.9.2 ...
[info] Done updating.
[info] Compiling 1 Java source to /home/mackler/tmp/javahello/target/scala-2.9.2/classes...
java.lang.RuntimeException: No main class detected.
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) No main class detected.
[error] Total time: 4 s, completed Sep 22, 2013 4:14:21 PM
mackler@localhost:~/tmp/javahello$

最佳答案

类的声明需要公开,否则sbt(和java)看不到:

package learn;                                                                                                         

public class HelloWorld { // added public here
public static void main(String[] args) {
System.out.println("Hello, world");
}
}

这有效。

> run
[info] Running learn.HelloWorld
Hello, world

编辑:事实上,该类不需要为 java 工作而公开,正如 Mark 指出的那样,这是 sbt 中的一个错误。

关于java - 为什么 SBT 检测不到 Java 中的主类? (未检测到主类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18948387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com