---
title: "Changing Java version procedure under linux"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Changing Java version procedure under linux}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Java installation

The JavaStics interface evolution from JavaSTICS-1.5.2 led to use a more recent Java virtual machine (at least version 17) which is not compatible with an older version (i.e. java 11).

If the default version of java is older than 17, a java 17 can be installed and solutions exist for using an alias or switching between versions as described in the next sections.

These information may be partially specific of installation procedures on Debian like distributions and can be easily adapted for other linux OS types.

## Creating an alias to java 17
This is the simplest solution and allows to avoid switching between versions. But, it is leading to use additional arguments for some R functions relying on JavaSTICS use in background (i.e. run_javastics, gen_xml2txt, for example).

<pre style="font-size: 11px">

alias java17=/usr/lib/jvm/java-17-openjdk-amd64/bin/java

</pre>

This can be added in the `.bashrc` file for permanent use.



## Switching between version

### Using the system tools

The following command under Ubuntu allows to manage switching easily

<pre style="font-size: 11px">
sudo update-alternatives --config java
</pre>


<pre style="font-size: 11px">
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection     Path                                            Priority  Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java      1111      auto mode
* 1            /usr/lib/jvm/java-11-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number:0

</pre>



## Using a shell script

After the Java 17 installation, and if it is not the default version known by the system, switching to this version can be done with a script like the following one. It can be ran either from R or a shell terminal.

```{bash, eval = FALSE}
#!/bin/bash

# Changing latest installed Java version to Java version 11
echo "The current Java version is : "
java -version

version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')

if ! [ -z $(echo $version | grep ^17) ];then
  echo "The Java version is already Java 17"
  exit 0
fi

echo "Changing Java version to Java 17"
if [ -e /usr/lib/jvm/java-11-openjdk-amd64/bin/java ];then
	     sudo rm /usr/bin/java
	     sudo ln -s /usr/lib/jvm/java-17-openjdk-amd64/bin/java /usr/bin/java
fi

version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')

if [ -z $(echo $version | grep ^17) ];then
  echo
  echo "Java 17 is not installed !"
  exit 1
fi

echo "The Java version is now : "
java -version

```


After that, 

> the **SticsOnR** package functions using the
> JavaSTICS 1.5.2 command line interface **can be used**.


## Switch back to the previous Java version on the system
For restoring the initial Java version, a script like the following can be ran. 


```{bash, eval = FALSE}
#!/bin/bash

# Changing to the previous installed Java version

echo "Current Java version is : "
java -version

version=$(java -version 2>&1 | awk -F '"' '/version/ {print $2}')

if [ -z $(echo $version | grep ^17) ];then
  echo
  echo "The Java version is not Java 17"
  exit 0
fi

#ls -ald /usr/lib/jvm/* | grep ^d | grep java-

last_version=$(/etc/alternatives/java -version 2>&1 | awk -F '"' '/version/ {print $2}')

if ! [ -z $(echo $last_version | grep ^17) ];then
  echo
  echo "No more recent version of Java is available on the system !"
  exit 0
fi
  
sudo rm /usr/bin/java
sudo ln -s /etc/alternatives/java /usr/bin/java
echo "JAVA version is now : "
java -version


```


