Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-
- Download VS Code and Apache Maven and install both.
- Install the Visual Studio extension pack for Java - e.g. by pasting this URL into a web browser:
vscode:extension/vscjava.vscode-java-packand then clicking on the green Install button after it opens in VS Code. - NOTE: See the comment from ADTC for an "Easier GUI version of step 3...(Skip step 4)." If necessary, the Maven quick start archetype could be used to generate a new Maven project in an appropriate local folder:
mvn archetype:generate -DgroupId=com.companyname.appname-DartifactId=appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false. This will create an appname folder with Maven's Standard Directory Layout (i.e.src/main/java/com/companyname/appnameandsrc/main/test/com/companyname/appnameto begin with and a sample "Hello World!" Java file named appname.javaand associated unit test named appnameTest.java).* - Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
- Open the Command Palette (via the View menu or by right-clicking) and type in and select
Tasks: Configure taskthen selectCreate tasks.json from template. - Choose maven ("Executes common Maven commands"). This creates a tasks.json file with "verify" and "test" tasks. More can be added corresponding to other Maven Build Lifecycle phases. To specifically address your requirement for classes to be built without a JAR file, a "compile" task would need to be added as follows:
{
"label": "compile",
"type": "shell",
"command": "mvn -B compile",
"group": "build"
},
- Save the above changes and then open the Command Palette and select "Tasks: Run Build Task" then pick "compile" and then "Continue without scanning the task output". This invokes Maven, which creates a
targetfolder at the same level as thesrcfolder with the compiled class files in thetarget\classesfolder.
Addendum: How to run/debug a class
Following a question in the comments, here are some steps for running/debugging:-
- Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
- Click on the green arrow in the Debug view and select "Java".
- Assuming it hasn't already been created, a message "launch.json is needed to start the debugger. Do you want to create it now?" will appear - select "Yes" and then select "Java" again.
- Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
- Click on the green arrow in the Debug view again.
Here is a complete list of steps - you may not need steps 1-3 but am including them for completeness:-
- Download VS Code and Apache Maven and install both.
- Install the Visual Studio extension pack for Java - e.g. by pasting this URL into a web browser:
vscode:extension/vscjava.vscode-java-packand then clicking on the green Install button after it opens in VS Code. - NOTE: See the comment from ADTC for an "Easier GUI version of step 3...(Skip step 4)." If necessary, the Maven quick start archetype could be used to generate a new Maven project in an appropriate local folder:
mvn archetype:generate -DgroupId=com.companyname.appname-DartifactId=appname-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false. This will create an appname folder with Maven's Standard Directory Layout (i.e.src/main/java/com/companyname/appnameandsrc/main/test/com/companyname/appnameto begin with and a sample "Hello World!" Java file named appname.javaand associated unit test named appnameTest.java).* - Open the Maven project folder in VS Code via File menu -> Open Folder... and select the appname folder.
- Open the Command Palette (via the View menu or by right-clicking) and type in and select
Tasks: Configure taskthen selectCreate tasks.json from template. - Choose maven ("Executes common Maven commands"). This creates a tasks.json file with "verify" and "test" tasks. More can be added corresponding to other Maven Build Lifecycle phases. To specifically address your requirement for classes to be built without a JAR file, a "compile" task would need to be added as follows:
{
"label": "compile",
"type": "shell",
"command": "mvn -B compile",
"group": "build"
},
- Save the above changes and then open the Command Palette and select "Tasks: Run Build Task" then pick "compile" and then "Continue without scanning the task output". This invokes Maven, which creates a
targetfolder at the same level as thesrcfolder with the compiled class files in thetarget\classesfolder.
Addendum: How to run/debug a class
Following a question in the comments, here are some steps for running/debugging:-
- Show the Debug view if it is not already shown (via View menu - Debug or CtrlShiftD).
- Click on the green arrow in the Debug view and select "Java".
- Assuming it hasn't already been created, a message "launch.json is needed to start the debugger. Do you want to create it now?" will appear - select "Yes" and then select "Java" again.
- Enter the fully qualified name of the main class (e.g. com.companyname.appname.App) in the value for "mainClass" and save the file.
- Click on the green arrow in the Debug view again.
An alternative way is to install the Maven for Java plugin and create a maven project within Visual Studio. The steps are described in the official documentation:
- From the Command Palette (Crtl+Shift+P), select Maven: Generate from Maven Archetype and follow the instructions, or
- Right-click on a folder and select Generate from Maven Archetype.
Videos
When you run java --version in integrated Terminal in VS Code, it will search JAVA_HOME set in Environment Variables, so it's still jdk1.8.
But when you run the file like helloworld.java, the execution scripts shown in Terminal should include C:\Program Files\Java\jdk-11.0.7+10_hotspot\bin\java.exe, which means java extension tools indeed uses jdk11 to build and compile projects.
About maven, you still need to add it to PATH, then mvn --version should work in Terminal:
If you set "maven.terminal.useJavaHome": true,, it will use JAVA_HOME in Environment Variables which is JDK1.8, you can specify jdk version with the following two ways:
When you create a maven project, you can choose jdk version manually;
In Maven projects' pom.xml, don't forget to rebuild the project:
<properties> <java.version>11</java.version> </properties>More information view Apache Maven Compiler Plugin – Setting the -source and -target of the Java Compiler.
Open Terminals by Java Version
Extension Pack for Java Auto Config
https://marketplace.visualstudio.com/items?itemName=Pleiades.java-extension-pack-jdk