Vscode build Java Development Run environment

Source: Internet
Author: User

With a period of time vscode, think can also, want to use Vscode integration of different development language, so studied the use of Vscode build Java environment, develop Java program. There are many posts on the Internet, but each person's experience is different, write down their experience.

Attached to the official website link, the official web site also on how to use the development of Java program in Vscode explained

Https://code.visualstudio.com/docs/languages/java

Vscode can develop different languages rely on is a lot of plug-ins, first download the development of Java Program Plug-in, the official web recommended plug-in has four:

1. Language Support for Java (TM) by Red Hat

2. Debugger for Java

3. Java Test Runner

4. Maven for Java

For the development of a simple Java program, only the first two can be done, but you need to manually build several configuration files (. classpath. Project, etc.), or some functions such as import, auto-complement and other useful features can not be used, can only check syntax errors. The third is to support JUnit unit testing, and the fourth one to support Maven's creation of Java projects.

After installing the plug-in, open file-preferences-settings, set parameters, configure Java and MAVEN paths, as shown below, the configuration is ready for Java development.

    "Java.home":"C:\\Program files\\java\\jdk1.8.0_172",//You can configure this item for Java development, and the following configuration is supported by Maven    "Maven.executable.path":"C:\\Program FILES\\MAVEN-3.5.4\\BIN\\MVN",    "java.configuration.maven.userSettings":"C:\\Program Files\\maven-3.5.4\\conf\\settings.xml",    "MAVEN.TERMINAL.CUSTOMENV": [        {            "environmentvariable":"Java_home",                  "value":"C:\\Program files\\java\\jdk1.8.0_172"        }    ]

Let's start with the Java operating environment that Vscode builds on, not using MAVEN, and then introducing MAVEN to create a Java project and develop it in Vscode.

First, Vscode the basic Java program development, do not use Maven

If you are developing a Java program that is simple, you can create only a few Java programs without creating a project, or you can manually create several configuration files to construct a project

1. Do not create a project, only a few Java applets developed

(1) Create a folder first work area

(2) Open the folder with Vscode, edit the Java code: Javatest.java and Peaple.java.

(3) After the compilation is complete, press F5 to prepare for debugging, then in the folder will generate the. Vscode folder, which generates Launch.json files and opens automatically. Modify the configuration item MainClass in the Launch.json file as shown in. If debugging different programs, MainClass every time to modify, a bit cumbersome.

(4) After the modification is completed, press F5 to debug and run successfully.

(5) The above code is a test, found a problem, such as just open Vscode, immediately press F5 Debug Javatest.java program, this is Vscode will report build failed, do you want to continue? After selecting proceed, the Peaple class is also reported to be missing.

However, if you run the Javatest.java before you open the Peaple.java class, let Vscode have such a class, and then run Javatest.java, will not report the above two problems.

Build failed, does want to continue? Problems sometimes occur, see crooked Nuts also encounter similar problems, do not know is not a bug.

Building Java Project shows error: "Build failed, do you want to continue?" : https://github.com/redhat-developer/vscode-java/issues/495

(6) Since the above code is only two files, Vscode will be prompted in the lower right corner as follows: Classpath is incomplete. Only syntax errors would be reported

Let's take a look at the "more information", the official explanation is that because these files do not belong to any one project, Vscode-java cannot get classpath and cannot report compilation errors, but can still report syntax errors, code navigation, and so on. This prompt will prompt each time, do not want to prompt the words can choose "Don t Show Again". If you want to avoid this problem, the code folder contains the Pom.xml (Maven configuration file) or Build.gradle (Gradle profile) or the default settings for Eclipse.

2. Manually create a Java project

(1) Create an empty folder for the workspace, and then create the SRC and target files in the workspace, as a source and destination folder

(2) The official language support for Java is through eclipse? JDT Language server,buildship to start a service listener and compile the source code, here we need to manually create two files to configure the compilation service related items:. Project and. classpath files

. procject File Contents:

<?XML version= "1.0" encoding= "UTF-8"?><projectdescription>    <name>JavaTest2</name>    <Comment>Project Testjava created by Buildship.</Comment>    <Projects>    </Projects>    <Buildspec>        <Buildcommand>            <name>Org.eclipse.jdt.core.javabuilder</name>            <arguments>            </arguments>        </Buildcommand>        <Buildcommand>            <name>Org.eclipse.buildship.core.gradleprojectbuilder</name>            <arguments>            </arguments>        </Buildcommand>    </Buildspec>    <Natures>        <Nature>Org.eclipse.jdt.core.javanature</Nature>        <Nature>Org.eclipse.buildship.core.gradleprojectnature</Nature>    </Natures></projectdescription>

. classpath File Contents

<?XML version= "1.0" encoding= "UTF-8"?><Classpath>    <ClasspathentryKind= "src"Path= "src"/>    <ClasspathentryKind= "Con"Path= "org.eclipse.jdt.launching.jre_container/org.eclipse.jdt.internal.debug.ui.launcher.standardvmtype/ javase-1.8/"/>    <ClasspathentryKind= "Con"Path= "Org.eclipse.buildship.core.gradleclasspathcontainer"/>    <ClasspathentryKind= "Output"Path= "Target"/></Classpath>

The first. project file, Vscode, identifies the project as the Eclipse Project, and the second file. CLASSPATH is configured with the source directory and the compiled output directory, and for externally imported packages, it is also configured in the. classpath file, as long as one row (or more lines) of configuration is added:

<kind= "Lib"  path= "Lib's address"/>

(3) If there are no. vscode folders and Launch.json can be created manually in the workspace. If you do not create it manually, the Vscode will also generate a template for modification when debugging.

Launch.json File Contents

{"  version": "0.2.0",  "Configurations": [    {      "type": "java",      "name": "Debug (Launch)",      " Request ":" Launch ",      " CWD ":" ${workspacefolder}/target ",      " sourcepaths ": [        " $ (workspaceroot)/src "      ],      "classpaths": [        "",        "$ (workspaceroot)/target"      ],      "MainClass": "${ Filebasenamenoextension} ",      " args ":" "    }  ]}

Where CWD is the root directory where the Javac command is configured to run, you should specify the root directory where the compiled. class file is located

Sourcepaths Configure the source directory, classpaths configure the compiled. Class directory, MainClass Configure the main class file to run

Args is the parameter to be added to run the class file and can be configured as needed

(4) Next compile and write Java code: Javatest2.java,peaple.java, note that there is no Classpath is incomplete error.

(5) Next compile and run Javatest2.java, we see that the. class files generated by the two classes are saved in the target folder

II. Create a Java project with MAVEN and develop it in Vscode

1. After installing MAVEN, configure, I installed to the C:\Program files\maven-3.5.4

In the system environment variable, add maven_home (or M2_home) whose value is C:\Program files\maven-3.5.4, and then the PATH environment variable is appended with ";%maven_home%\bin"

Configuration complete detection, enter mvn-version in command control line, will output MAVEN version and JDK version, if you want to have JDK

2. Configuring Maven in Vscode

    "Maven.executable.path":"C:\\Program FILES\\MAVEN-3.5.4\\BIN\\MVN",    "java.configuration.maven.userSettings":"C:\\Program Files\\maven-3.5.4\\conf\\settings.xml",    "MAVEN.TERMINAL.CUSTOMENV": [        {            "environmentvariable":"Java_home",                  "value":"C:\\Program files\\java\\jdk1.8.0_172"        }    ]

3. Create a workspace, use the command control line to enter the workspace, create a project framework with the MVN command: MVN archetype:generate

Maven commands See: Https://www.cnblogs.com/yjmyzz/p/3495762.html#commentform

We created a simple MAVEN project with the project name JavaTest3 and the package named Test,maven the creation of the project framework.

The framework is structured as follows:

JAVATEST3├──POM.XML└──SRC     └──main     │   └──java     │      └──test     │          └──app.java     └── Test         └──java            └──test                └──apptest.java

4. Open the JavaTest3 folder with Vscode, do not do anything, Vscode will automatically put. Project,. classpath files and. Setting folder and the contents of it, and even generated the. class file

5. Press F5 to debug, modify the Launch.json file MainClass "test". APP "to run successfully.

In addition, when not maven, Build failed, do you want to continue? The problem happens, and then continue to follow.

  

Reference article:

Java in VS Code:https://code.visualstudio.com/docs/languages/java

Building Java Project shows error: "Build failed, do you want to continue?" :https://github.com/redhat-developer/vscode-java/issues/495

"Classpath is incomplete" warning:https://github.com/redhat-developer/vscode-java/wiki/%22classpath-is-incomplete %22-warning

Maven Learning (UP)-Basic Getting Started usage: https://www.cnblogs.com/yjmyzz/p/3495762.html#commentform

Java vscode Manual Import Jar:https://www.jianshu.com/p/ed1deb28aab9

VS code + Java configuration and use: 80298326

Configuring Vscode Debugging Java Code: https://www.codercto.com/a/12779.html

The following two links antivirus software reported virus, carefully opened

http://blog.hotsun168.com/index.php/archives/10/

http://blog.hotsun168.com/index.php/archives/16/

Vscode build Java Development Run environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.