使用javac
从命令行编译Java类时,使用.java
,但尝试在类中运行.class
方法时不使用main
java
命令。
例如,为了编译一个名为 HelloWorld 的类,我们使用javac HelloWorld.java
,但要运行同一个类(假设它有main方法),我们编写java HelloWorld
和不是java HelloWorld.class
。为什么会这样?生成的.class
文件具有相同类的字节码,但仍然只传递名称。
答案 0 :(得分:2)
正如java
的{{3}}所说:
<强>概要强>
java [ options ] class [ argument... ]
java [ options ] -jar file.jar
<强>参数强>
options Command-line options.
class Name of the class to be invoked.
file.jar Name of the jar file to be invoked. Used only with the -jar option.
argument Argument passed to the main function.
注意,class
部分,因此您将类名传递给java
,而不是类文件。