指定在通过 Visual Studio Code 导出 JAR 时按项目使用的 Java 版本。

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

Specify java version by project when exporting JAR via Visual Studio Code

问题

我已成功在VSCode的设置中配置了java.configuration.runtimes,以指定我想要用于编译项目的Java版本,并且可以成功导出已编译的JAR文件。我需要知道的是,如果我有多个项目,想要使用不同版本的Java进行导出,是否有这个选项?

似乎java.configuration.runtimes设置是在IDE级别上而不是项目级别上的。

英文:

I have successfully configured java.configuration.runtimes in the settings of VSCode in order to specify what version of Java I want to use to compile a project, and can successfully export a compiled JAR. What I need to know is, if I have multiple projects that I want to export using different versions of Java, is this an option?

It appears the java.configuration.runtimes setting is at the IDE level and not at the project level.

答案1

得分: 0

可以在导出项目为JAR文件时通过以下设置指定Java版本:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-11",
    "path": "/path/to/jdk11"
  },
  {
    "name": "JavaSE-14",
    "path": "/path/to/jdk-14",
    "default": true
  }
]

设置中的 default: true 将决定编译项目时使用的Java版本。

通过上述设置,我创建了一个项目,按下 F5 进行编译和运行:
指定在通过 Visual Studio Code 导出 JAR 时按项目使用的 Java 版本。

你可以看到Java扩展使用jdk14来编译 .java 文件,并转到下划线下面的目录,那里有一个 .class 文件。

使用以下命令:

javap -verbose .class

会显示很多信息,其中 major version 的值也代表Java版本:
指定在通过 Visual Studio Code 导出 JAR 时按项目使用的 Java 版本。

JDK11 的主要版本是 55,而 JDK14 的主要版本是 58,所以该设置确实起作用。你可以将它添加到用户设置中,然后使用不同的Java版本导出项目为JAR文件。

英文:

Right, you can specify java version when exporting projects as jars via the setting:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-11",
    "path": "/path/to/jdk11",
  },
  {
    "name": "JavaSE-14",
    "path": "/path/to/jdk-14",
    "default": true
  },
]

The setting default:true's location will decide the java version when you compile the project.

With the above setting, i create a project, when pressing F5 to compile and run:
指定在通过 Visual Studio Code 导出 JAR 时按项目使用的 Java 版本。

you can see the java extension did use jdk14 to compile the .java file, and turn to the directory of the words yellow underlined, there is a .class file.

Using the command:

javap -verbose .class

there will be a lot of information showed and the value of major version also represents java version:
指定在通过 Visual Studio Code 导出 JAR 时按项目使用的 Java 版本。

the major version of JDK11 is 55, and the one of JDK14 is 58, so the setting does work. You can add it in User setting, then export projects as jars with different java version.

huangapple
  • 本文由 发表于 2020年9月12日 01:14:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63851548.html
匿名

发表评论

匿名网友

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

确定