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
- 安裝VSCode(略)
- 安裝VSCodeJavaInstaller(只支持Windows)
VSCodeJavaInstaller-online-win-0.2.2.exe
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 StartedOpen 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