使用 javac<file> 和 java<file> 的区别
Difference between using javac<file> and java<file>
来自一些有经验的 java coder 我想问一下使用
有什么区别
javac <filename>
java <file_name_without_extention>
和
java <filename>
如果您的源代码只是一个文件,它们是等价的。前者(用两个命令)是编译和运行 Java源代码的一般方式,它仍然是编译更大项目的正确方式。后者是 JDK 11 中添加的新功能,可以更轻松地 运行 单个文件和非常小的程序。
来自the proposal that suggested the feature
In source-file mode, the effect is as if the source file is compiled
into memory, and the first class found in the source file is executed.
For example, if a file called HelloWorld.java
contains a class called
hello.World
, then the command
java HelloWorld.java
is informally equivalent to
javac -d <memory> HelloWorld.java
java -cp <memory> hello.World
javac <Filename> - a java command that compiles java source files into bytecodes.
它需要一个扩展名,因为你正在编译源文件。
java -cp <classpath> <Classname> - a java command that executes the compiled bytecodes.
它不需要扩展,因为您只是告诉它从要执行的类路径中搜索 Class 及其 main() 签名。
来自一些有经验的 java coder 我想问一下使用
有什么区别javac <filename>
java <file_name_without_extention>
和
java <filename>
如果您的源代码只是一个文件,它们是等价的。前者(用两个命令)是编译和运行 Java源代码的一般方式,它仍然是编译更大项目的正确方式。后者是 JDK 11 中添加的新功能,可以更轻松地 运行 单个文件和非常小的程序。
来自the proposal that suggested the feature
In source-file mode, the effect is as if the source file is compiled into memory, and the first class found in the source file is executed. For example, if a file called
HelloWorld.java
contains a class calledhello.World
, then the commandjava HelloWorld.java
is informally equivalent to
javac -d <memory> HelloWorld.java java -cp <memory> hello.World
javac <Filename> - a java command that compiles java source files into bytecodes.
它需要一个扩展名,因为你正在编译源文件。
java -cp <classpath> <Classname> - a java command that executes the compiled bytecodes.
它不需要扩展,因为您只是告诉它从要执行的类路径中搜索 Class 及其 main() 签名。