Skip to content

Instantly share code, notes, and snippets.

@badsyntax
Last active August 24, 2023 09:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save badsyntax/4c66a0f09f545320e52bb4a559777a88 to your computer and use it in GitHub Desktop.
Save badsyntax/4c66a0f09f545320e52bb4a559777a88 to your computer and use it in GitHub Desktop.
Getting started with Java development on macOS

Getting started with Java development on macOS

Install multiple Java versions

To start, list current installed Java versions:

/usr/libexec/java_home -verbose

Install homebrew: https://brew.sh/

Tap and list java versions:

brew tap homebrew/cask-versions
brew search java
brew search jdk

Install latest Java version:

brew cask info java
brew cask install java

Install Java8:

brew cask info adoptopenjdk/openjdk/adoptopenjdk8
brew cask install adoptopenjdk/openjdk/adoptopenjdk8

List installed Java versions:

/usr/libexec/java_home -verbose

Project setup

Create new project dir:

mkdir example-project
cd example-project

Install direnv:

brew install direnv

Add 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 -version

Install gradle:

brew install gradle

Create a new Java project:

gradle init --type java-application --dsl groovy --test-framework junit

You 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"

VS Code Setup

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.

Custom Java extension settings

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
}

Running the application

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment