IntelliJ vs Gradle JDK版本

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

IntelliJ vs Gradle JDK versions

问题

请问有人可以解释一下Gradle的java块中指定的Java版本与IntelliJ IDEA项目结构对话框中指定的Java版本之间的区别吗?

我知道用于执行Gradle本身的JVM是在设置中指定的,所以不是那个。我假设问题中的程序是为Gradle中的targetCompatibility构建的,但如果是这种情况,那么项目结构对话框又定义了什么呢?

英文:

Can someone explain the difference between the Java version specified in Gradle's java block vs in the IntelliJ IDEA Project Structure dialog?

IntelliJ vs Gradle JDK版本

I know the JVM that's used to execute Gradle itself is specified in Settings, so neither is that. I assume the program in question is being built for targetCompatibility in Gradle, but if that is the case what does the Project Structure dialog define?

答案1

得分: 1

在理论上,Gradle的targetCompatibility设置与项目结构的设置是相同的事情。

让我们来看一下javac命令的输出:

--release
编译为指定的Java SE版本。支持的版本:8、9、10、11、12、13、14、15、16、17、18、19、20

--source ,-source
与指定的Java SE版本提供源代码兼容性。支持的版本:8、9、10、11、12、13、14、15、16、17、18、19、20

每个JDK版本都有一个发布版本号,通常情况下,较高版本的JDK(以20为例)可以编译和运行所有较低版本的代码。

然而,如果你正在使用JDK 8,你只能编译Java 8的源代码,并且无法编译Java源代码级别为9、10等,也只能运行Java 8的类。

每个类文件都有一个内部版本号,显示了它可以在最低的JVM版本上运行,这是由javac --release参数的值指定的。

所以回到Gradle,截图表明该项目至少使用JDK 8来编译和运行它,因此IDEA将选择一个真正的JDK 17来编译和运行它。当然,JDK 17可以编译和运行更近期的Java源代码级别,包括Java 8。

我为这个概念绘制了一个小图像:
IntelliJ vs Gradle JDK版本

英文:

In theory, the Gradle targetCompatibility settings are the same thing as the Project Structure's settings.

Let's take a look at the javac command output:

  --release <release>
        Compile for the specified Java SE release. Supported releases: 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
  
  --source <release>, -source <release>
        Provide source compatibility with the specified Java SE release. Supported releases: 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

Every JDK version has a release number, and usually, the higher version of JDK(take 20 for example), can compile and run all lower-release versions.

However, if you are using JDK 8, you can only compile Java 8 sources level and can't compile Java source level 9, 10, etc, and also can only run Java 8 class.

Every class file has an internal version number, which shows the least JVM version it can be run at, this is specified by the javac --release arg value.

So back to Gradle, the screenshot suggests that the project at least uses JDK 8 to compile and run it, so the IDEA will pick a real JDK 17 to compile and run it. Sure the JDK 17 can compile and run more recent Java source code levels including Java 8.

I've drawn a small image for that concept:
IntelliJ vs Gradle JDK版本

答案2

得分: 0

"Project Structure"对话框定义了用于IntelliJ IDEA的Java版本,例如代码补全或实时显示编译错误的目的。简而言之,它用于编码。例如,如果在“Project Structure”中选择“<NO SDK>”,您仍然可以运行Gradle,但无法编写代码。为了让您有机会在您想要的Java版本上编写代码,您可以在“Project Structure”中选择语言级别,但它仍然用于编码,而不是使用Gradle进行编译。

英文:

The Project Structure dialog defines the version of Java that is being used for purposes of Intellij IDEA, e.g. for code completion or to show you compilation errors in real time. In short, it is used for coding. For example, if you choose &lt;NO SDK&gt; in the Project Structure you will still be able to run Gradle but you won't be able to write a code. And to give you the opportunity to write a code on the version of Java that you want, you can choose the language level in the Project Structure, but it will still be used for coding, not for compilation using Gradle.

huangapple
  • 本文由 发表于 2023年7月27日 23:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781235.html
匿名

发表评论

匿名网友

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

确定