为什么.class不与'java'命令一起使用?

时间:2015-02-09 11:09:27

标签: java bytecode .class-file

使用javac从命令行编译Java类时,使用.java,但尝试在类中运行.class方法时不使用main java命令。

例如,为了编译一个名为 HelloWorld 的类,我们使用javac HelloWorld.java,但要运行同一个类(假设它有main方法),我们编写java HelloWorld和不是java HelloWorld.class。为什么会这样?生成的.class文件具有相同类的字节码,但仍然只传递名称。

1 个答案:

答案 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,而不是类文件。