在运行 .class 文件和 .java 文件之间是否有实质性的区别?

huangapple go评论72阅读模式
英文:

Is there a meaningful difference between running .class or .java?

问题

我开始学习Java,并阅读了Oracle的《适用于Microsoft Windows的“Hello World!”》指南。在指南中,它告诉你使用javac编译为.class文件,然后用以下方式运行:

java -cp . HelloWorldApp

当我尝试运行时,我没有运行正确的文件,而是意外地运行了

java helloworldapp.java

注意到后,我尝试了最初的方式,它们都打印出

Hello World!

这让我思考,以编译的.class文件形式运行与以原始源代码 .java 文件形式运行是否有任何区别?

英文:

I'm starting Java, and was reading the "Hello World!" for Microsoft Windows guide from Oracle. In the guide, it tells you to use javac to compile into a .class, then run with

java -cp . HelloWorldApp

When I tried running it, I didn't run the proper file and instead accidentally ran

java helloworldapp.java

After I noticed that, I tried the original way and they both printed

Hello World!

This got me thinking, is there any difference to running it as a compiled .class vs the original source code .java?

答案1

得分: 3

在Java 11中,现在可以运行'java <source_file>',主要是为了帮助熟悉这门语言:https://openjdk.java.net/jeps/330

在幕后,它首先编译源文件,然后运行编译后的类。对于更简单的用例(例如:一个文件的Java程序,没有依赖项),行为可能是相同的,但值得注意的是,这并不意味着一般情况下要取代'先编译,然后执行'。

英文:

In Java 11, it is now possible to run 'java <source_file>' mostly as a way to help gain familiarity with the language: https://openjdk.java.net/jeps/330

Behind the scenes it is first compiling the source file then running the compiled class. For simpler use cases (ex: 1 file java program, with no dependencies) the behavior is likely to be the same, but it is worth noting that this is not meant as a replacement of 'compile then execute' in general.

答案2

得分: 2

在作为已编译的.class文件运行和原始源代码.java文件之间几乎没有什么区别。

然而,直接运行.java源文件是一种便利,但也有一些限制:

  • 最严重的限制是它仅支持单源程序。如果您在多个源文件中有类,您无法使用此功能。

  • 仅对指定的编译器选项有有限支持。对于更高级的用途,您需要直接调用编译器。

  • 重新运行程序需要重新编译源代码,因此启动程序会稍微较慢。

但是,除了这些限制,实际上没有任何区别,除了只需要运行一个命令的便利性。

当然,由于您应该使用**集成开发环境(IDE)**来开发Java程序,这实际上并不重要。这意味着该特性恢复到其最初的目的,如JEP 330: 启动单文件源代码程序中所述,即运行#! shebang脚本的目的。

英文:

There is little difference between running it as a compiled .class vs the original source code .java.

However, the ability to run a .java source file directly is a convenience, and has limitations:

  • The most severe limitation is that it only supports single-source programs. If you have classes in multiple source files, you cannot use this feature.

  • There is only limited support for specifying compiler options. For more advanced use, you need to invoke the compiler directly.

  • Re-running the program requires re-compiling the source, so starting the program will be a little slower.

But, other than those limitations, there is really no difference, except the convenience of only having to run one command.

Of course, since you should be developing Java programs using an IDE, it really doesn't matter. Which means that the feature reverts to it's original purpose, as specified in JEP 330: Launch Single-File Source-Code Programs, i.e. that of running #! shebang scripts.

huangapple
  • 本文由 发表于 2020年9月19日 00:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63959775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定