title | allowTitleToDifferFromFilename | shortTitle | intro | redirect_from | versions | type | topics | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Setting up a Java project for GitHub Codespaces |
true |
Setting up a Java project |
Get started with a Java project in {% data variables.product.prodname_github_codespaces %} by creating a custom dev container configuration. |
|
|
tutorial |
|
This guide shows you how to set up an example Java project {% data reusables.codespaces.setting-up-project-intro %}
{% data reusables.getting-started.sign-in-dotcom %}
- Go to https://github.com/microsoft/vscode-remote-try-java. {% data reusables.codespaces.use-this-template %}
When you create a codespace, your project is created on a remote virtual machine that is dedicated to you. By default, the container for your codespace has many languages and runtimes, including Java. It also includes a set of commonly used tools such as Gradle, Maven, git, wget, rsync, openssh, and nano.
{% data reusables.codespaces.customize-vcpus-and-ram %}
The default development container, or "dev container," for {% data variables.product.prodname_github_codespaces %} will allow you to work successfully on a Java project like vscode-remote-try-java. However, we recommend that you configure your own dev container to include all of the tools and scripts your project needs. This will ensure a fully reproducible environment for all {% data variables.product.prodname_github_codespaces %} users in your repository.
{% data reusables.codespaces.setup-custom-devcontainer %} {% data reusables.codespaces.command-palette-container %}
-
Type
java
and click the Java option. Other options are available if your project uses particular tools. For example, Java & PostgreSQL. -
Choose the version of Java you want to use for your project. In this case, select the version marked "(default)."
-
Select the option to Install Maven and click OK.
-
A list of additional features you can install is displayed. We'll install Ant, the Java library and command-line tool for building applications. To install this feature, type
ant
, selectAnt (via SDKMAN)
, then click OK.
{% data reusables.codespaces.overwrite-devcontainer-config %} {% data reusables.codespaces.details-of-devcontainer-config %}
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/java
{
"name": "Java",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none",
"installMaven": "true",
"installGradle": "false"
},
"ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {}
}
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "java -version",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
{% data reusables.codespaces.devcontainer-properties-1 %} {% data reusables.codespaces.devcontainer-properties-2 %}
{% data reusables.codespaces.additional-container-config %}
With your dev container configuration added and a basic understanding of what everything does, you can now make changes to customize your environment further. In this example, you'll add properties that will:
- Run a command, after the dev container is created, to create a new file.
- Automatically install two {% data variables.product.prodname_vscode_shortname %} extensions in this codespace.
-
In the
devcontainer.json
file, add a comma after thefeatures
property."features": { "ghcr.io/devcontainers/features/java:1": { "version": "none", "installMaven": "true", "installGradle": "false" }, "ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {} },
-
Uncomment the
postCreateCommand
property and change its value toecho \"This file was added by the postCreateCommand.\" > TEMP.md
.// Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "echo \"This file was added by the postCreateCommand.\" > TEMP.md",
-
Uncomment the
customizations
property and edit it as follows to install the "Code Spell Checker" extension and the "Extension Pack for Java."// Configure tool-specific properties. "customizations": { // Configure properties specific to VS Code. "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ "streetsidesoftware.code-spell-checker", "vscjava.vscode-java-pack" ] } }
The devcontainer.json
file should now look similar to this, depending on which image you chose:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/java
{
"name": "Java",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
"image": "mcr.microsoft.com/devcontainers/java:0-17",
"features": {
"ghcr.io/devcontainers/features/java:1": {
"version": "none",
"installMaven": "true",
"installGradle": "false"
},
"ghcr.io/devcontainers-contrib/features/ant-sdkman:2": {}
},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "echo \"This file was added by the postCreateCommand.\" > TEMP.md",
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"streetsidesoftware.code-spell-checker",
"vscjava.vscode-java-pack"
]
}
}
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
{% data reusables.codespaces.save-changes %} {% data reusables.codespaces.rebuild-command %} {% data reusables.codespaces.rebuild-reason %}
After the dev container is rebuilt, and your codespace becomes available again, the postCreateCommand
will have been run, creating a TEMP.md
file, and the two extensions will be available for use.
-
Run the application by pressing
F5
. -
If a "toast" notification message is displayed at the bottom right corner of {% data variables.product.prodname_vscode_shortname %}, asking whether you want to switch to standard mode, click Yes.
-
When the project files have been imported, click the Debug Console tab to see the program output.
{% data reusables.codespaces.committing-link-to-procedure %}
You should now be able to add a custom dev container configuration to your own Java project.
{% data reusables.codespaces.next-steps-adding-devcontainer %}