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.