To start, list current installed Java versions:
/usr/libexec/java_home -verboseInstall homebrew: https://brew.sh/
Tap and list java versions:
brew tap homebrew/cask-versions
brew search java
brew search jdkInstall latest Java version:
brew cask info java
brew cask install javaInstall Java8:
brew cask info adoptopenjdk/openjdk/adoptopenjdk8
brew cask install adoptopenjdk/openjdk/adoptopenjdk8List installed Java versions:
/usr/libexec/java_home -verboseCreate new project dir:
mkdir example-project
cd example-project
Install direnv:
brew install direnvAdd the following line at the end of the ~/.bashrc file:
eval "$(direnv hook bash)"Edit the local env file:
direnv edit .Set the Java version for this project:
export JAVA_HOME=$(/usr/libexec/java_home -v11.0.2)Enable direnv for this project:
direnv allow .Check the Java version:
java -versionInstall gradle:
brew install gradleCreate a new Java project:
gradle init --type java-application --dsl groovy --test-framework junitYou probably want to install the shadowJar plugin: https://plugins.gradle.org/plugin/com.github.johnrengelman.shadow
Now build the project:
./gradlew build
Commit initial files:
git init
git add -A
git commit -m "Add initial files"Install VS Code: https://code.visualstudio.com/
Open VS Code, open the command pallete, search for and select "install code command in path". Close VS Code.
Now open the project in VS Code from your terminal: code .
Open up the extensions panel, and search for and install "java extension pack" by Microsoft. This will install a bunch of recommended Java extensions. It can take a while for all the extensions to be installed. Once installed, reload VS Code to activate the extensions.
Open VS Code settings (Preferences >> Settings).
- Set "Java › Configuration: Update Build Configuration" to be "automatic".
- Set "Java › Implementations Code Lens" to "Enabled"
- Set "Java › References Code Lens" to "Enabled"
- Set "Java › Save Actions: Organize Imports" to "Enabled"
- Set "Breadcrumbs" to "Enabled"
Show config
{
"java.configuration.checkProjectSettingsExclusions": false,
"workbench.colorCustomizations": {
"statusBar.background": "#000000",
"statusBar.noFolderBackground": "#000000"
},
"files.trimTrailingWhitespace": true,
"editor.renderWhitespace": "all",
"java.configuration.updateBuildConfiguration": "automatic",
"java.implementationsCodeLens.enabled": true,
"java.referencesCodeLens.enabled": true,
"java.saveActions.organizeImports": true,
"breadcrumbs.enabled": true
}VS Code will use use the Java version specified in $JAVA_HOME, so there's no additional configuration required.
Open up build.gradle and save it without making any changes. You should see text in the status bar indicating VS Code syncing the Java project. You should see a thumbs up icon in the bottom right of the status bar once completed. VS Code will update depedenecies whenever you make changes to the build.gradle file.
You should now be able to open the App.java file, and Run/Debug the main method via the Code Lens.
You should also be able to open the AppTest.java file and Run/Debug the test via the Code Lens. Once a test has completed, you can click on result icon next in the Code Lens to view a Test Report. To debug output from the tests, open up the "Output" tab in the debug panel, and select the "Java Test Runner".
You can add custom run arguments in the launch.json file, for example:
{
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - App",
"request": "launch",
"mainClass": "example.project.App",
"projectName": "example-project",
"args": "arg1 arg2"
}
]
}
Happy Java programming!