使用 VSCode 和 Javac 编译 Java 项目,而非 JDT 编译器

250 阅读2分钟

在 Java 开发中,Eclipse JDT 编译器(Java Development Tools)是许多 IDE 默认使用的编译器。然而,某些情况下,我们可能希望使用 javac 编译器来进行更传统的 Java 编译。本文将介绍如何在 Visual Studio Code (VSCode) 中配置和使用 javac 来编译 Java 代码,而不是依赖 JDT 编译器。

前提条件

  1. 已安装 VSCode。
  2. 已安装 JDK,并配置了 JAVA_HOME 环境变量和 PATH 环境变量。
  3. 安装了必要的 VSCode 扩展:Java Extension PackMaven for Java

安装 Java Extension Pack

打开 VSCode,进入扩展视图(按 Ctrl+Shift+X),搜索并安装 Java Extension Pack

安装 Maven for Java

同样在扩展视图中,搜索并安装 Maven for Java

配置 Maven 使用 javac 编译器

在 VSCode 中,我们将通过 Maven 来调用 javac 编译代码。首先,确保你的项目配置正确的 Maven 构建脚本。

配置 pom.xml

编辑你的 pom.xml 文件,添加或更新 Maven Compiler Plugin 配置,以使用 javac 编译器:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerId>javac</maven.compiler.compilerId>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <fork>true</fork>
                    <executable>${JAVA_HOME}/bin/javac</executable>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

配置 VSCode 任务

为了在 VSCode 中使用 javac 编译代码,我们可以通过配置 tasks.json 文件来实现。

创建或打开 tasks.json

在你的项目根目录下,创建 .vscode 文件夹(如果尚未存在)。在 .vscode 文件夹中创建或打开 tasks.json 文件。

添加 Maven 编译任务

tasks.json 文件中添加以下配置:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Maven: clean",
            "type": "shell",
            "command": "mvn clean",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": ["$maven"],
            "detail": "Run Maven clean"
        },
        {
            "label": "Maven: compile",
            "type": "shell",
            "command": "mvn compile",
            "group": "build",
            "problemMatcher": ["$maven"],
            "detail": "Run Maven compile"
        },
        {
            "label": "Maven: package",
            "type": "shell",
            "command": "mvn package",
            "group": "build",
            "problemMatcher": ["$maven"],
            "detail": "Run Maven package"
        },
        {
            "label": "Maven: install",
            "type": "shell",
            "command": "mvn install",
            "group": "build",
            "problemMatcher": ["$maven"],
            "detail": "Run Maven install"
        },
        {
            "label": "Maven: test",
            "type": "shell",
            "command": "mvn test",
            "group": "test",
            "problemMatcher": ["$maven"],
            "detail": "Run Maven tests"
        }
    ]
}

运行 Maven 任务

运行任务

打开命令面板(按 Ctrl+Shift+P),然后输入 Run Task 并选择它。选择你在 tasks.json 中配置的任务,例如 Maven: compile

配置调试

为了调试编译后的代码,我们可以配置 launch.json 文件。

创建 launch.json

打开命令面板(按 Ctrl+Shift+P),然后输入 Debug: Open launch.json 并选择它。选择 Java 以创建一个 Java 调试配置文件。

配置 launch.json

确保 launch.json 配置正确指向你的编译输出。例如:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch) - Current File",
            "request": "launch",
            "mainClass": "${file}",
            "console": "internalConsole"
        },
        {
            "type": "java",
            "name": "Debug (Launch) - Maven Build",
            "request": "launch",
            "mainClass": "com.example.MainClass",
            "preLaunchTask": "Maven: package",
            "console": "internalConsole"
        }
    ]
}

在这个示例中,Debug (Launch) - Maven Build 配置会在调试之前运行 Maven: package 任务,确保最新的代码已经被编译和打包。

总结

通过上述步骤,你可以在 VSCode 中配置使用 javac 编译代码,并使用 tasks.jsonlaunch.json 管理构建和调试任务。