Skip to content

Instantly share code, notes, and snippets.

@lemonteaa
Created August 8, 2022 22:34
Show Gist options
  • Save lemonteaa/e667b0db0c6527d4aed9518f91d00af8 to your computer and use it in GitHub Desktop.
Save lemonteaa/e667b0db0c6527d4aed9518f91d00af8 to your computer and use it in GitHub Desktop.
Java Setup

Revision: Setting up Java dev env with code-server + sdkman

Context

A trend in recent years is the rise of remote development. One important component of it is the use of isolated container/VM (possibly ephemeral) for the sole purpose of development. Moreover, all the setups necessary beforehand are made standardised and repeatable using scripts/IoC etc. When done right, this drastically reduces friction to on-board developer joining a project by making the "Getting Started" process as simple as clicking a button - one is then led to a webpage running vscode, backed up by a container/VM running in the cloud, with everything already setup and ready to use.

In this note, we will perform the steps in setting up a standard Java development enviornment manually. This can be useful in a pinch - you got your hands on a VPS/plain vscode service, but which lack those automation/integrations.

Installing, configuring, and exposing code-server

(You may skip this step if you already have access to a running instance)

Reference: https://github.com/coder/code-server

Just run

curl -fsSL https://code-server.dev/install.sh | sh

to install. Then to start run

code-server

In the default case, during first run a config file will be generated and placed in the directory shown. You should read that file for the password - this will be needed when you visit the webpage containing vscode.

In general, to actually start using it, you would need to expose it on either your private network (eg by SSH port forwarding), or the public internet.

For scripting purpose, the following items may be useful (quoting from official docs):

You can change the config file's location using the --config flag or $CODE_SERVER_CONFIG environment variable.

code-server --install-extension <extension id>
# example: code-server --install-extension wesbos.theme-cobalt2

# From the Coder extension marketplace
code-server --install-extension ms-python.python

# From a downloaded VSIX on the file system
code-server --install-extension downloaded-ms-python.python.vsix

Also note that for many web-based env providing their own tunnel, you need to bind to 0.0.0.0 instead of 127.0.0.1. In this case you can directly override it with:

code-server --bind-addr 0.0.0.0:8080

Installing Java and Maven via sdkman

sdkman is a no-fuse version manager for the programming language runtime, as well as related toolings. This is a nice thing:

  • Installing a programming language manually can be a tedious task, so this automates it away from you (though you still need to install this tool first)
  • In professional context it is common to get into version conflicts that forces one to use a specific version of the programming language runtimes (This is especially true for Java)

So yes this is a two-stage process that might be off-putting to beginners. On the other hand for a small investment it prevents many possible headaches/eliminates a class of risk down the road.

Installing is straight forward:

sudo apt-get update
Sudo apt-get install zip unzip
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

Then we should install Java and Maven. To do so interactively, first examine the versions available to choose from:

sdk list java

(Similar for Maven)

Once you've chosen your version, install with:

sdk install java <version-id>

By default, it will set the first version you install as the default and activate it. (On subsequent installs it will ask you)

Please note down the path of the executables - this will be used later:

which java
which mvn

Java Extension Pack for vscode

The Java Extension Pack is a bundle of the most useful/important extensions relating to the Java language.

To install:

(Note: OpenVSX marketplace is used due to license issue)

Note that depending on the exact version of vscode you're using, installation may fail due to version incompatibility. A workaround is to install an older, compatible version of the extension manually:

  • Go to website https://open-vsx.org and search the offending extension.
  • Identify the right version and select it, then note the download URL.
  • Download on the machine with wget or curl etc.
  • In vscode, click "TODO"

Interlude: vscode Settings

A downside of using sdkman is that vscode won't be able to auto-detect your installation and so you need to tell it explicitly where the binaries are.

  • Click extension, select the extensions, then click settings.
  • Find the relevant config option
  • Enter the path

(Note: for java.home it is listed only in the "Remote" but not "User" section.)

Creating a Java Project

Open the command palette, then type "Java" or "Project". Choose: TODO.

Select "Use Maven"

Then select the maven archetype. A maven archetype is like a template to scaffold a project in other programming language.

it can be confusing to find one suitable for you in a sea, so I suggest:

I use two archetypes. It depends on what kind of application you will create. If you want a web application, use maven-archetype-webapp, or if you want a simple application use maven-archetype-quickstart. They are useful because you will be able to expand them with no problem.

Open any Java source file - this will trigger the Java language extension to analyze your code (it runs a javac process behind the scene), and this is what makes it possible to give you some basic intellisense-like feature. This will take some time, so please be patient. When done, you will see a list of Java class + methods of your current file in the "Outline" panel:

The Java Project Extension will also analyze your project and give you a navigation view based on the Java classes instead of having to open all those folders (as Java class is required to follow the folder hierarchy, deeply nested folders that are difficult for human to manually navigate is natural)

(You may need to manually add your src/main/java folder to the path for it to kick in)

Run and build

Although the Java Language Extension itself supports running your project, I recommend sticking to doing everything through Maven as it is considered the "authoritative" view on what your deliverables/binaries actually is.

Again, open command palette, then "Maven: Execute Command". Select the project, then select your goal (most common one is install).

It takes a bit more work to run a command line java program with maven. In short, add the exec-plugin plugin in your pom.xml file:

TODO

On the command line:

mvn exec:java -Dmain

Or inside vscode itself, while choosing goal, click "Custom", then type "exec:java".

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