英文:
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 进行编译和运行:
你可以看到Java扩展使用jdk14来编译 .java 文件,并转到下划线下面的目录,那里有一个 .class 文件。
使用以下命令:
javap -verbose .class
会显示很多信息,其中 major version 的值也代表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:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论