# JAVA 基础开发环境 vscode 搭建

### 开始

1. 在 Visual Studio Code 中打开扩展视图(Ctrl+Shift+X)。

2. 输入“java”以过滤扩展列表。

3. 找到并安装 Java Extension Pack (Java 扩展包)，如果你已经安装了 Language Support for Java(TM) by Red Hat，也可以单独找到并安装 Java Debugger for Visual Studio Code 扩展。

1. 准备项目。打开一个 .java 文件，Java 扩展会激活。Maven、Gradle 和 Eclipse 项目都能得到支持。这个扩展会自动构建项目，不需要手工触发构建。

2. 开始调试。切换到调试视图(Ctrl+Shift+D)，打开 launch.json 并添加 Java 调试配置。

3. 在 mainClass 中填入要启动的类，或者在 hostName 中填入要附加的主机名以及在 port 中填入端口。

4. 设置断点并按 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.

## 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.

1. In Visual Studio Code, go to the Extensions view (Ctrl+Shift+X).
2. 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:

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.

## Editing and Navigating Code

### Linting

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 HatIntellisense 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)
• 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 over F10, Step into F11, Step out Shift+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:

1. Switch to the Debug view (Ctrl+Shift+D).
2. Open launch.json to add a debug configuration for Java
3. Fill in the mainClass for Launch setting or hostName and port for Attach.
4. Click Start button or press F5 to start debugging.

## Next Steps

• Basic Editing - Learn about the powerful VS Code editor.
• Debugging - find out how to use the debugger with your project

Windows下VSCode编译运行简单java

1. JDK & JRE 的配置（不细讲了网上一大堆）
2. VSCode的下载安装以及插件cpptools的安装（不细讲了，见blackkitty的另一篇文章中的步骤1和2:Windows下VSCode编译调试c/c++
5. 完了！

3. 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,
}
]
}


{
"version": "0.1.0",
"command": "cmd",
"args": [
"/c", "start", "cmd","/c",
"hide:true",            //设为hide:true则会清除编译信息，否则将保留编译信息
"${fileDirname}", "${fileBasename}",
"javac -Xlint:unchecked -encoding UTF-8 -g"     //javac默认部分编译参数
]
}

::::::::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.完了！

posted @ 2018-01-13 18:44  lsgxeva  阅读(29365)  评论(0编辑  收藏  举报