PRC2

Getting Started

To work successfully with the programs needed for PRC2, you need to install them first. Properly installing Java and other programs is not hard but must be done precisely. What will follow is a description on how to do that under Ubuntu Linux, macOS and Windows. You can adapt this configuration for other operating systems too, possibly with a few tweaks.

Warning:

Before you install any software from the internet, please validate the the package. You typically see signature files such as SHA-256 or PGP-ASC, os something similar, which can help to ascertain that the package is the one you think it is. For instance with:

$ sha512sum /path/to/file
$ shasum -a 256 /path/to/file
$ certUtil -hashfile /path/to/file SHA256

You can check the download of that version of netbeans, and the command should produce the same signature as is available on the download website.

Setting up the correct environment

Install Java

The default installation paths are:

/usr/lib/jvm
/Library/Java/JavaVirtualMachines/
C:\Program Files\AdoptOpenJDK\

Warning:

Do NOT use Program Files under Windows, because of the space in the name, but instead create a separate directory simply called e.g. c:\usr\lib\jvm if you insist on using Windows. On other OSs it is also important to make sure paths do NOT contains spaces, as spaces are also used as separators for arguments

Installation of Java can be done either through a package manager or by downloading the the Java distribution from AdoptOpenJDK . For PRC2 we will use Java 21.

Linux

With a package manager:

install openjdk via apt-get on ubuntu
sudo apt install openjdk-21-jdk
install eclipse temurin via apt-get
apt-get install -y wget apt-transport-https gnupg
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | apt-key add -
echo "deb https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
apt-get update # update if you haven't already
apt-get install temurin-21-jdk

Manual installation:

In the example below we assume that the distribution is packed as a tar.gz archive. The example shows the archive name as of 21 Januari 2024, OpenJDK21U-jdk_x64_linux_hotspot_21.0.2_13.tar.gz

unpack java distribution using the command-line
sudo mkdir -p /usr/lib/jvm
cd /usr/lib/jvm
sudo tar xf ~/Downloads/OpenJDK21U-jdk_x64_linux_hotspot_21.0.2_13.tar.gz

macOS

With HomeBrew:

install via HomeBrew
# Untap AdoptOpenJDK first
brew untap AdoptOpenJDK/openjdk

# If you want to download latest version
brew install --cask temurin

# To install specific version
brew tap homebrew/cask-versions
brew install --cask temurin21

Manual installation:

Download the correct distribution/version of choice from AdoptOpenJDK . Install the same way as normal programs.

Windows

Install with the Windows Package Manager:

First install the winget

install via powershell
winget install Microsoft.OpenJDK.21

Manual installation:

Download the correct distribution/version of choice from AdoptOpenJDK . Install the same way as normal programs,

Warning:

Do NOT forget to change the install path during installation.
e.g. C:\usr\lib\jvm\
Also make sure to check Add to path and Add JAVA HOME.

Make Java available on the PATH

If you used a package manager or an installer to install Java, then Java should already be added to your path. However if you manually extracted the binaries then you should still add Java to the path.

The $PATH environment variable is used by the command line processor to find the commands or programs whose name you type as first word in a command line. If you type java -version, and your shell says command not found, then your path does not include the java program yet.

Since we want the best Java experience, we will use some common environment variables.

Linux

JAVA_HOME Define JAVA_HOME first. That will help Java, but also tell other programs to find the desired Java version.

Define JAVA_HOME
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 1
export JAVA_HOME=/usr/lib/jvm/temurin-17-jdk 2
  1. Path for manual installation
  2. Path for package manager installation

PATH Prepend the directory under JAVA_HOME to your path, so all java related programs, such as java , javac, jar etc can be found, by just typing the name.

Append Java to the path
export PATH=${JAVA_HOME}/bin:$PATH

To avoid having to do this every time you start the command-line, add the commands to your /.bashrc//.zshrc script file, at the bottom.

Add JAVA_HOME to PATH
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=${JAVA_HOME}/bin:$PATH

To switch Java versions use the following command:

Switch Java versions
sudo update-alternatives --config java

macOS

These instructions will guide you to setup the correct environment on macOS and to make it easy to switch between Java versions.

List version(s) of Java
/usr/libexec/java_home
/usr/libexec/java_home -V

The first command lists the most recent version of Java on your system The second command lists all installed versions of Java on your system

We can now setup an environment to easily switch between Java versions and later on even launch Netbeans with the intended Java version. The only thing we need to do for this is set the JAVA_HOME environment variable to link to the correct Java version.

Set the JAVA_HOME environment
export JAVA_HOME=<path_to_java_home>

Where the <path_to_java_home> can be obtained by the command above with the -V parameter.

Check that JAVA_HOME is set correctly
echo $JAVA_HOME

However instead of hardcoding the path it is better to retrieve the path while setting the environment.

Set JAVA_HOME dynamically with the following command (e.g. for Java 15):
export JAVA_HOME=$(/usr/libexec/java_home)

$() means that the invocation between parenthesis is resolved first.

This sets the correct Java environment for your current Terminal session. When opening a new terminal the default Java environment will be active again. To make switching between Java versions in the terminal easier we can set-up what is called an alias. An alias is basically a shortcut, you specify the keyboard shortcut and which commands should be executed. We want these aliases to always be available, for this we need to let the terminal know to load our aliases on startup. Depending on the type of terminal you are using, we need the specify the aliases at different places.

Since macOS Catalina (10.15) the default shell (which is used by the terminal) is ZShell (zsh). This means we can specify our aliases in either .zshrc or in .zprofile in our home directory. We suggest to create an additional configuration file .java_setup in your home directory.

Add the following lines to ~/.zshrc:
# Include java environment setup
source .java_setup
Add the following lines to ~/.java_setup:
# Set your default JAVA_HOME
export JAVA_HOME=$(/usr/libexec/java_home -v 17.0.6)

alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11.0.12`; java -version"
alias j17="export JAVA_HOME=`/usr/libexec/java_home -v 17.0.6`; java -version"
alias j21="export JAVA_HOME=`/usr/libexec/java_home -v 21.0.2`; java -version"

# Add maven to path
export PATH=/Applications/maven/apache-maven-3.9.0/bin:$PATH

We now need to either restart the terminal for the aliases to take effect or source the .zshrc file:

source ~/.zshrc

We can now check that switching between versions works:

switching between Java versions
java -version
j11
j17
j21

Check that the default Java version is correct Check that switching between versions works Check that switching between versions works Check that switching between versions works

Now we have set-up a way to easily switch between Java versions in the terminal.

Windows

Java should already be added to the path by the installer, as it is a default setting during installation.

Install Apache Maven

We will use Apache Maven, or maven for short throughout the PRC2 course, so you will need that too.

The application is called Maven but as command spelled as mvn.

Maven is the default build tool in the Java world and can build your program from sources without the use of and IDE. This makes your project agnostic to the IDE used, so they work with NetBeans as first class projects out of the box and also with other IDEs with little or no tweaks to that IDE. It is also the way the teachers use to compile and test the students work for the practical assignments and performance assessments.

Linux

Install with package manager:

Install maven using apt-get
sudo apt install maven

Install manually:

Fetch a fresh copy of Apache Maven from the website.

To install it, do (example uses version 3.9.6 as of 2024-01-21)

install apache maven and add to path
cd /usr/share
sudo rm -fr maven
sudo tar xf ~/Downloads/apache-maven-3.9.6-bin.tar.gz
sudo ln -sf apache-maven-3.9.6/ maven
cd /usr/bin
sudo ln -sf ../share/maven/bin/mvn .

Once this is done, entering mvn --version should produce output.

macOS

Install with HomeBrew:

Install Maven using HomeBrew
brew install maven

Install manually:

Fetch a fresh copy of Apache Maven from the website. Unzip the archive at an appropriate location (e.g. /Applications). Make sure to put maven on the path, see java_setup.

Once this is done, entering mvn --version should produce output.

Windows

Install with Chocolately:

choco install maven

Install manually:

  1. Download the zip file from https://maven.apache.org/download.cgi.
  2. Extract the zip file to C:\usr\lib
  3. Add the bin directory to the path
    1. Goto Settings > About > Advanced System Settings > Adavanced Tab > Environment Variables
    2. Select Path and Edit
    3. New and fill in the path C:\usr\lib\apache-maven-3.6.3\bin
  4. Close System Properties
  5. Start cmd
  6. mvn -v should return a version

Install or update your IDE

The official Java IDE at Fontys ICT Venlo is Apache NetBeans IDE. The latests version for the moment is 20.0.

Linux

You can either install Netbeans from Ubuntu Software (Snap) or download the binaries.

To install the binaries of Apache NetBeans on Ubuntu simply fetch the binary as zip file and unpack it, then either add the contained bin directory to the path, or make a wrapper netbeans command in your personal ~/bin directory.

For my installation I simply used the binaries, in the zip file, and install it in the traditional (as in same directories as the installer would) /usr/local. /opt is also a good choice as installation directory.

Assuming you downloaded the binary, installing it is easy
cd /usr/local
sudo unzip ~/Downloads/netbeans-20-bin.zip
sudo mv netbeans{,-21}

For extra creature comfort, add a simple netbeans script to your path

netbeans script in ~/bin
#!/usr/bin/env bash
/usr/local/netbeans-20/bin/netbeans "$@" & 1
  1. Invoke netbeans using its absolute path.

Then make that script executable with chmod +x netbeans.

NetBeans IDE should now be startable from the command line (fine for me) as well as from the menu.

macOS

It is possible to install Netbeans by downloading the installer, manual extracting the binaries or by using HomeBrew.

Installing with the installer:

  1. Download the installer from https://netbeans.apache.org/download/index.html
  2. Install as normal
Install Netbeans using HomeBrew
brew install --cask netbeans

Installing the binaries manually, see installation guidelines for maven on MacOS.

As an extra we can start Netbeans from the command-line using the Java version we want to use.

We do this by adding an executable file to the PATH so we can start Netbeans from the terminal with the correct Java version.

Create netbeans file in /usr/local/bin
cd /usr/local/bin 1
touch netbeans 2
echo \"/Applications/NetBeans/Apache\ NetBeans\ 21.app/Contents/Resources/NetBeans/netbeans/bin/netbeans\" --jdkhome
\$JAVA_HOME \$\* \& > netbeans 3
chmod +x netbeans 4
  1. Change to the right directory
  2. Create new file called netbeans
  3. Write the correct command into netbeans
  4. Make the file executable

As you can see, when invoking it this way, you can inform Netbeans about which java version to use (as set in your JAVA_HOME environment variable). Furthermore you can pass the project name as a parameter (defined by $*`) so you don’t need to open the project manually anymore in NetBeans. Finally the & means that NetBeans runs as a background process without blocking your Terminal session, so your Terminal session is immediately available again.

Open myProject in Netbeans using Java version 17
j17
netbeans myProject

Windows

  1. Download the installer from https://netbeans.apache.org/download/index.html
  2. Install as usual, make sure to change to path

Other IDE’s

We use NetBeans IDE as default. We might not be able to answer any support questions on other IDE’s.

Additional Pointers