JAVA 基礎開發環境 vscode 搭建 Windows下VSCode編譯運行簡單java
JAVA 基礎開發環境 vscode 搭建
來源 https://www.cnblogs.com/freewsf/p/7744728.html對於使用 Visual Studio Code 的 Java 開發者來說,Language Support for Java(TM) by Red Hat 擴展提供了非常好的語言特性支持,比如智能感知和項目支持等。然而,我們也聽到一些用戶反饋需要調試 Java 程序。今天,我們萬分激動地宣布,我們與 Red Hat 合作開發了能讓開發者調試 Java 應用程序的輕量級擴展:Java Debugger for Visual Studio Code,它基於 Java Debug Server。
註意: Java Debugger for Visual Studio Code 和 Java Debug Server 都將在發布初始版後不久開放源碼。
為了讓 Java 開發者能盡快開始使用 VS Code,我們也創建了一個 Java 擴展包,它包含了 Language Support for Java(TM) by Red Hat 和 Java Debugger for Visual Studio Code 這兩個擴展,這樣你不需要單獨去搜索安裝這些擴展。這是剛剛開始創建的一個現代 Java 工作流程,將來我們會往 Java 擴展包中添加更多特性和擴展。
開始
從以下步驟開始:
-
在 Visual Studio Code 中打開擴展視圖(Ctrl+Shift+X)。
-
輸入“java”以過濾擴展列表。
-
找到並安裝 Java Extension Pack (Java 擴展包),如果你已經安裝了 Language Support for Java(TM) by Red Hat,也可以單獨找到並安裝 Java Debugger for Visual Studio Code 擴展。
你也可以直接從本文中安裝 Java 擴展包:
安裝 Java 擴展包 (安裝了 VSCode 的情況下,點擊上面的鏈接即可調用 VSCode 進行安裝)
重啟 VSCode,打開包含 Java 項目的代碼,然後按下面的步驟進行:
-
準備項目。打開一個 .java 文件,Java 擴展會激活。Maven、Gradle 和 Eclipse 項目都能得到支持。這個擴展會自動構建項目,不需要手工觸發構建。
-
開始調試。切換到調試視圖(Ctrl+Shift+D),打開 launch.json 並添加 Java 調試配置。
-
在 mainClass 中填入要啟動的類,或者在 hostName 中填入要附加的主機名以及在 port 中填入端口。
-
設置斷點並按 F5 開始調試。
支持的特性
在這個版本中,我們支持以下特性:
-
啟動/附加 - 你可以在 VS Code 中啟動一個 Java 項目,也可以附加到一個在本地或遠程端以調試模式運行的 JVM 進程。
-
斷點 - 支持表達式條件或命中條件斷點,可以在內置的斷點設置容器中輕松進行設置。因此你可以直接在源代碼視圖中插入斷點,不需要彈出容器,非常方便。按異常中斷也得到了支持。
-
控制流 - 包括 暫停、繼續 F5、單步 F0、步進 F11、步出(跳出) Shift+F11。
-
數據審查 - 當程序停止在斷點上的時候,調試器可以訪問當前內存中的變量名和值。支持查看/監視/設置變量值。
-
診斷 - 調用棧 面板會顯示程序的調用棧,你在這個面板中可以在捕獲的調用路徑中隨意跳轉。多線程調試也通過並行棧得到支持。
-
調試控制臺 - 可以從調試控制臺看到標準輸出和錯誤輸出信息。
The Java support in VS Code is provided through extensions and optimized for lightweight Java projects with a simple, modern workflow. Popular extensions in the Marketplace provide project support, code completion, linting, debugging, code formatting, snippets, and more.
Download VS Code - If you haven‘t downloaded VS Code yet, quickly install for your platform (Windows, Mac, Linux).
Install Java Extensions
VS Code is a fast editor and ships with only the basic features. Add Java language support to VS Code by installing the popular Java extensions.
- In Visual Studio Code, go to the Extensions view (Ctrl+Shift+X).
- Filter the extensions list by typing "java".
To help set up Java on VS Code, there is a Java Extension Pack which contains two popular extensions:
- Language Support for Java(TM) by Red Hat
- Java Debugger for Visual Studio Code
Install the Java Extension Pack
This document describes the features included in the Java Extension Pack.
Java Project Support
Maven, Eclipse and Gradle Java project are supported through Language Support for Java(TM) by Red Hat, by utilizing M2Eclipse which provides Maven support and Buildship which provides Gradle support through the Eclipse JDT Language Server.
Please note that Gradle-based Android projects are not supported.
Editing and Navigating Code
Linting
A linter is a tool that provides warnings for suspicious looking code. Language Support for Java(TM) by Red Hat provides this feature to report parsing and compilation errors as you type, so you can fix them inside VS Code.
Intellisense
VS Code also supports code completion and Intellisense for Java through Language Support for Java(TM) by Red Hat. Intellisense is a general term for a number of features, including intelligent code completion (in-context method and variable suggestions) across all your files and for built-in and thirty-party modules.
Search for symbols
You can search for symbols in the current file or workspace to navigate your code more quickly.
To search for a symbol in the current file, use Quick Open (Ctrl+P) then enter the ‘@‘ command, then enter the name of the symbol you‘re looking for. A list of potential matches will appear and be filtered as you type. Choose from the list of matches to navigate to its location.
To search for a symbol in the current workspace, start by pressing Ctrl+T, then enter the name of the symbol. A list of potential matches will appear as before. If you choose a match that was found in a file that‘s not already open, the file will be opened before navigating to the match‘s location. Alternatively, you can also use Quick Open (Ctrl+P) then enter the ‘#‘ command to search the current workspace.Ctrl+T is just the shortcut for the ‘#‘ commands, respectively, so everything works the same.
Peek Definition
You can take a quick look at how a symbol was defined by using the Peek Definition feature. This feature displays a few lines of code near the definition inside a peek window so you can take a look without navigating away from your current location.
To peek at a symbol‘s definition, place your cursor on the symbol anywhere it‘s used in your source code and then press Alt+F12. Alternatively, you can choose Peek Definition from the context menu (right-click, then choose Peek Definition).
Go to Definition
You can also quickly navigate to where a symbol is defined by using the Go to Definition feature.
To go to a symbol‘s definition, place your cursor on the symbol anywhere its used in your code and then press F12. Alternatively, you can choose Go to Definition from the context menu (right-click, then chooseGo to Definition). When there‘s only one definition of the symbol, you‘ll navigate directly to its location, otherwise the competing definitions are displayed in a peek window as described in the previous section and you have to choose the definition that you want to go to.
Other code editing features include:
- Code formatting
- Code snippets
- Code outline
- Code lens (references)
- Javadoc hovers
- Rename
- Highlights
Debugging
Java Debugger for Visual Studio Code is a lightweight Java Debugger based on Java Debug Server. It works with Language Support for Java by Red Hat to allow users debugging Java code using Visual Studio Code (VS Code).
The Java Debugger supports following features:
- Launch/Attach - You can either launch the Java project within VS Code or attach to any running JVM process in debug mode, locally or remotely.
- Breakpoints - Conditional breakpoints by Hit Count is supported and can easily be set using the inline breakpoint settings window. This allows you to conveniently add conditional breakpoints to your code, directly in the source viewer, without requiring a modal window. Break on exceptions is also supported.
- Control flow - Including Pause, Continue
F5
, Step overF10
, Step intoF11
, Step outShift+F11
- Data inspection - When you‘re stopped at a breakpoint, the debugger has access to the variable names and values that are currently stored in memory. Inspect/Watch/Set Variables are supported.
- Diagnostics - The CALL STACK panel shows the call stack of your program and allows you to navigate through the call path of each captured allocation. Multi-threaded debugging is supported by parallel stacks.
- Debug Console - The Debug Console lets you see information from both stdout and stderr.
You can launch the debugger following steps below:
- Switch to the Debug view (Ctrl+Shift+D).
- Open
launch.json
to add a debug configuration for Java - Fill in the
mainClass
forLaunch
setting orhostName
andport
forAttach
. - Click Start button or press
F5
to start debugging.
Next Steps
Read on to find out about:
- Basic Editing - Learn about the powerful VS Code editor.
- Code Navigation - Move quickly through your source code.
- Tasks - use tasks to build your project and more
- Debugging - find out how to use the debugger with your project
Windows下VSCode編譯運行簡單java
註意:vscode當前並不支持對java的調試,此文僅是配置使用vscode編譯運行簡單java,blackkitty並不寫java所以此文僅供娛樂
先看效果:
配置流程:
- JDK & JRE 的配置(不細講了網上一大堆)
- VSCode的下載安裝以及插件cpptools的安裝(不細講了,見blackkitty的另一篇文章中的步驟1和2:Windows下VSCode編譯調試c/c++ )
- launch.json與tasks.json的配置
- vsc_tasks_java.bat的配置
- 完了!
務必確保1,2步驟已經完成。
3. launch.json的配置
首先打開一個文件夾,新建一個java文件,寫入代碼:
註意:代碼第一行必須是運行時要啟動class的名字,格式為兩個斜杠加class名
然後按F5,選擇C++(GDB):
將自動生成的launch.json內容替換為一下內容並保存:
{
"version": "0.2.0",
"configurations": [
{
"name": "JAVA Launch",
"type": "cppdbg",
"request": "launch",
"launchOptionType": "Local",
"targetArchitecture": "x86",
"program": "${file}",
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"externalConsole": true,
"preLaunchTask": "cmd"
}
]
}
切換至debug界面,點擊調試,註意這裏必須切換至debug界面(因為第一次要先選擇一個調試類型),不能直接按F5,在第一次編譯運行後,以後的編譯運行可以直接按F5:
此時會彈出信息框,點擊”配置任務程序”:
在這裏隨便選一個:
然後用下面代碼替換(註意看註釋,有些可以自己調整):
{
"version": "0.1.0",
"command": "cmd",
"args": [
"/c", "start", "cmd","/c",
"vsc_tasks_java.bat",
"hide:true", //設為hide:true則會清除編譯信息,否則將保留編譯信息
"${fileDirname}",
"${fileBasename}",
"javac -Xlint:unchecked -encoding UTF-8 -g" //javac默認部分編譯參數
]
}
4.vsc_tasks_java.bat的配置
將以下代碼保存為vsc_tasks_java.bat文件,然後復制到C:\Program Files (x86)\Java\jdk1.8.0_91\bin\文件夾下,註意與你自己的jdk所在位置對應,並確保環境變量設置正確
::::::::VSCODE TASKS JAVA BY BLACKKITTY::::::::
@echo off
set jfdir=%2
set jf=%3
set jccmd=%~4
set jclassdir=%jfdir%\%jf%.class
set/p jstartclass=<"%jfdir%\%jf%">NUL
set jstartclass=%jstartclass:~2%
echo.jfdir:%jfdir%
echo.jf:%jf%
echo.jccmd:%jccmd%
echo.jclassdir:%jclassdir%
echo.jstartclass:%jstartclass%
echo.---------------------------
echo.CMD EXECUTE: mkdir "%jclassdir%"
if not exist "%jclassdir%" mkdir "%jclassdir%"
echo.CMD EXECUTE: %jccmd% -d "%jclassdir%" "%jfdir%\%jf%"
(%jccmd% -d "%jclassdir%" "%jfdir%\%jf%") || set jcerror=true
if "%jcerror%" EQU "true" (pause&goto:eof)
pushd "%jclassdir%"
echo.-----------CLASS-----------
dir /b *.class
echo.-----------CLASS-----------
echo.CMD EXECUTE: java "%jstartclass%"
echo.---------------------------
if "%1" EQU "hide:true" (cls)
java "%jstartclass%"
pause
goto:eof
::::::::VSCODE TASKS JAVA BY BLACKKITTY::::::::
完成後再次啟動調試,可見程序成功編譯運行:
不顯示編譯信息的:
顯示編譯信息的:
6.完了!
再次聲明:vscode當前並不支持對java的調試,此文僅是配置使用vscode編譯運行簡單java,blackkitty並不寫java所以此文僅供娛樂
JAVA 基礎開發環境 vscode 搭建 Windows下VSCode編譯運行簡單java