VSCode编译调试Java代码

VSCode编译调试Java代码

本文参考了Writing Java with Visual Studio Code系列文章。操作下来很简单。系统是 Windows7 x64,目录:C:\DEVPACK (名字凭心情,不要带空格,只用简单英文)。留足300GB空间。

安装系统软件

  • 安装 JDK1.8
    如果采用 jdk-8u162-windows-x64.exe 安装,可以安装完,得到jdk1.8.0_162目录内容,复制到DEVPACK/java/目录下,然后再卸载掉jdk。这样jdk不会总是提示自动更新。然后配置环境变量。

  • 安装 Maven
    解压 apache-maven-3.6.3-bin.zip 到 DEVPACK/

  • 设置系统环境变量

DEVPACK_HOME=C:\DEVPACK
JAVA_HOME=%DEVPACK_HOME%\java\jdk1.8.0_162
JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8
JRE_HOME=%JAVA_HOME%\jre
MAVEN_HOME=%DEVPACK_HOME%\apache-maven-3.3.3
MAVEN_OPTS=-Xms256m -Xmx512m -Dfile.encoding=UTF-8
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
Path=(原有内容);%JAVA_HOME%\bin;%JRE_HOME%\bin;%MAVEN_HOME%\bin

然后打开 cygwin 测试:

$ java -version

java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8

$ mvn -version

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: C:\DEVPACK\apache-maven-3.3.3
Java version: 1.8.0_162, vendor: Oracle Corporation
Java home: C:\DEVPACK\java\jdk1.8.0_162\jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "windows 7", version: "6.1", arch: "amd64", family: "dos"
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
Configure Components

Required components are listed below. Click Install to proceed.

    Java Development Kit:  8        Installed
    Visual Studio Code:    1.42.1   Installed

Actions:

    Configure Java Settings
    Install Java Extensions

如果是Ubuntu,要分别安装Java扩展,大致如下:

Language Support for Java(TM) by Red Hat
Debugger for Java
Java Test Runner
Maven for Java
Java Dependency Viewer

编写一个java程序(仅学习)

  • 简单的java项目
    VSCode Command: Ctrl+Shift+P -> Java: Getting Started

    Open folder: hellojava
    Hello.java

  • eclipse 工程
    VSCode Command: Ctrl+Shift+P -> Java: Create Java Project

    选择目的文件夹:Projects/
    给项目起个名字: HelloJava2

  • maven 工程
    VSCode Command: Ctrl+Shift+P -> Maven: Create Maven Project

编写一个java-maven项目(实践中建议使用这种方法)

用命令行创建一个maven工程:

$ mvn archetype:generate -DarchetypeCatalog=internal -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=com.github.hellomvn -DartifactId=hellomvn -Dpackage=com.github.hellomvn -Dversion=1.0

pom.xml中增加下面的内容指定java版本:

<project>
    ......
  	<properties>
  		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  		<java.version>1.8</java.version>
  	</properties>
  	<dependencies>
  	... ...
  	</dependencies>
  	<build>
  	    <plugins>
  	        <!-- 修改maven默认的JRE编译版本(默认配置1.5), 这里更改为1.8 -->
  	        <plugin>
  	            <groupId>org.apache.maven.plugins</groupId>
  	            <artifactId>maven-compiler-plugin</artifactId>
  	            <version>2.3.2</version>
  	            <configuration>
  	                <source>1.8</source>
  	                <target>1.8</target>
  	            </configuration>
  	        </plugin>
  	    </plugins>
  	</build>
</project>

然后用 VSCode打开项目hellomvn,cmd:

cd hellomvn
code .

java-maven项目测试

使用Java Test Runner扩展,上面的过程已经安装了 Java Test Runner。Java Test Runner支持以下测试框架:

  • JUnit 4 (v4.8.0+)
  • JUnit 5 (v5.1.0+)
  • TestNG (v6.8.0+)

本文使用JUnit5。

  • pom.xml 修改后的全文如下
    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.github.hellomvn</groupId>
      <artifactId>hellomvn</artifactId>
      <packaging>jar</packaging>
      <version>1.0</version>
      <name>hellomvn</name>
      <url>http://maven.apache.org</url>
    
      <properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    	    <java.version>1.8</java.version>
    	    <maven.compiler.source>${java.version}</maven.compiler.source>
    	    <maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
    	    <junit.jupiter.version>5.3.0</junit.jupiter.version>
    	</properties>
    
      <dependencies>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-api</artifactId>
          <version>${junit.jupiter.version}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-params</artifactId>
          <version>${junit.jupiter.version}</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>${junit.jupiter.version}</version>
          <scope>test</scope>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
              <source>${java.version}</source>
              <target>${java.version}</target>
            </configuration>
          </plugin>
    
          <plugin>
            <!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
          </plugin>
        </plugins>
      </build>
    </project>
    
  • AppTest.java 修改后的全文如下
    package com.github.hellomvn;
    
    import static org.junit.jupiter.api.Assertions.*;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    
    
    /**
     * JUnit5 test for simple App.
     */
    public class AppTest {
        @BeforeEach
        void init() {
            System.out.println("AppTest init");
        }
    
        @AfterEach
        void tearDown() {
            System.out.println("AppTest tearDown");
        }
    
        @DisplayName("AppTest testFirstFunction")
        @Test
        void testFirstFunction() {
            assertEquals(100, 100);
        }
    
        @DisplayName("AppTest testSecondFunction")
        @Test
        void testSecondFunction() {
            assertTrue( true );
        }
    }
    

现在,你已经具备了用maven建立java工程,用junit5测试,按F5调试。用vscode编辑调试测试的全套工具集。是时候抛弃eclipse了。
详细的测试部分参考下面的讲解:
Testing Java with Visual Studio Code

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章