Include the java -version command in bash script - java

[Message updated to include script and its output as you asked].
I have created a linux script which installs Oracle Java on Ubuntu.
The script is the following:
#!/bin/bash
# This script installs Oracle Java 7u51 jdk on Ubuntu Linux 64 bit
echo "Starting Oracle Java JDK Installation..."
sudo mkdir /usr/local/java
cd
sudo cp -r jdk-7u51-linux-x64.tar.gz /usr/local/java
rm jdk-7u51-linux-x64.tar.gz
cd /usr/local/java
echo "Unpacking java files in /usr/local/java"
sudo tar xvzf jdk-7u51-linux-x64.tar.gz
sudo rm jdk-7u51-linux-x64.tar.gz
sleep 1s
echo "Updating system Path file /etc/profile with Java variables"
sudo sed -i '$ a\JAVA_HOME=/usr/local/java/jdk1.7.0_51' /etc/profile
sudo sed -i '$ a\PATH=$PATH:$HOME/bin:$JAVA_HOME/bin' /etc/profile
sudo sed -i '$ a\export JAVA_HOME' /etc/profile
sudo sed -i '$ a\export PATH' /etc/profile
sleep 1s
echo "Updating alternatives"
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/
jdk1.7.0_51/bin/java" 1
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/
jdk1.7.0_51/bin/javac" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/
jdk1.7.0_51/bin/javaws" 1
sudo update-alternatives --set java /usr/local/java/jdk1.7.0_51/bin/java
sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_51/bin/javac
sudo update-alternatives --set javaws /usr/local/java/jdk1.7.0_51/bin/javaws
sleep 3s
echo "Reload system wide Path /etc/profile"
. /etc/profile
sleep 3s
echo "Testing if Java is installed correctly. System must reply with Java version."
java –version
sleep 1s
javac -version
The script runs fine doing its thing and Java is installed correctly.
But the java -version command at the end gives this output at the terminal.
Testing if Java is installed correctly. System must reply with Java version.
Error: Could not find or load main class –version
javac 1.7.0_51
Two things baffle me.
The first is that the javac -version commands works in the script but not the java -version command.
The second is that if I go to the terminal right after the script has ended and input myself java -version, it works.

The install may be adding to the PATH, which you won't see from inside the current shell. Try it with
sh -c java -version

Maybe, you can change the calling order, you can call the reload system wide Path operation before the update-alternatives operations.

Related

I can't install lombok in Spring Tool Suite 4 ide on Ubuntu

I'm trying to install lombok in the Spring Tool Suite 4 ide which is in the /opt folder on Ubuntu. The command: java -jar lombok.jar does not work because the opt folder requires privileges.
I tried using the command suggested by the installation software: sudo java -jar lombok.jar I get the result:
sudo: java: command not found
Note: I have the SDKMAN software development kit manager installed to manage java versions.
Because root does not have sdkman install. root can not get java.
You can do this:
sudo su -
mkdir -p /usr/lib/jvm
cd /usr/lib/jvm
ln -s /home/YourUserName/.sdkman/candidates/java/current jdk
nano /root/.bashrc
# ADD This
export JAVA_HOME=/usr/lib/jvm/jdk
export PATH=/usr/lib/jvm/jdk/bin:$PATH
# Ctrl + O save
# Ctrl + X exit nano
exit
UPDATE:
Add JDK Path to sudoers config:
Run Command : sudo nano /etc/sudoers
find Defaults secure_path="/usr/local/sb..."
append :/usr/lib/jvm/jdk/bin at end of this line
it will like Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/usr/lib/jvm/jdk/bin"
save and exit nano.
Test: sudo java -version
Then you can run sudo java -jar lombok.jar

Update gitlab JDK to JDK17 (VM doesn't have internet connection)

I am using Gitlab to build a Java tool using ant
The tool requires JDK 17, but ant JDK version is 11, and I'm trying to change it.
So I tried a lot of solutions using a remote repository or remote download site, but after some tries I found out that the VM used to build the tool is not connected to internet (trying to ping google or my IP address doesn't work).
So I tried to upload in the same package with the tool source code the JDK 17 (openjdk-17_linux-x64_bin.tar.gz) and install it there.
Here is the problem, I am not sure how to do this since I don't work with linux, but I tried almost everything on the internet.
Every of these commands are used in a .gitlab-ci.yml file, used for gitlab pipeline.
Here are some examples of what I've tried so far:
- sudo cp /builds/project/openjdk-17_linux-x64_bin.tar.gz /usr/lib/jvm
- sudo tar zxvf "/usr/lib/jvm/openjdk-17_linux-x64_bin.tar.gz" -C /usr/lib/jvm
- echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /etc/profile
- echo "PATH=${PATH}:${HOME}/bin:${JAVA_HOME}/bin" | sudo tee -a /etc/profile
- echo "export JAVA_HOME" | sudo tee -a /etc/profile
- echo "export JRE_HOME" | sudo tee -a /etc/profile
- echo "export PATH" | sudo tee -a /etc/profile
- sudo cat /etc/profile
- echo "JAVA_HOME=/usr/lib/jvm/jdk-17" | sudo tee -a /.bashrc
- echo "PATH=${PATH}:${JAVA_HOME}/bin" | sudo tee -a /.bashrc
- echo "JAVA_HOME='/usr/lib/jvm/jdk-17' | sudo tee -a /etc/environment"
- export JAVA_HOME=/usr/lib/jvm/jdk-17
- export PATH=$PATH:$JAVA_HOME/bin
After a lot of combinations of these commands the output of sudo update-alternatives --config java is still:
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2deb10u1)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2deb10u1, mixed mode, sharing)
But if I try /usr/lib/jvm/jdk-17/bin/java -version it prints 17.
What would be the solution of making the default Java version to be 17. (Also a solution for ant to use the JDK-17 without installing it would be great too, since I need the JDK-17 for ant)
Since you've already found a way to change the jdk on-the-go, you may really want to consider change the base image of your CI to save yourself a lot of time. This step will boost your CI speed. The steps to do that is fairly simple too.
Compose your own Dockerfile
This following is just a pesudo code. You may look into the description of dockerfile builder
FROM your-original-image. This is what you have in your image tag in the gitlab-ci file.
COPY jdk-17-linux-x64.tar.gz /usr/lib/jvm
RUN sudo tar zxvf "/usr/lib/jvm/jdk-17-linux-x64.tar.gz" -C /usr/lib/jvm \
&& sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-1.11.0-openjdk-amd64 \
&& sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/default-java \
&& sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-11-openjdk-amd64 \
&& sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/openjdk-11 \
&& sudo update-alternatives --remove-all java \
&& sudo update-alternatives --remove-all javac \
&& sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
Build the docker image
If you are using docker hub, then you need to login to docker and get a dockerId which matches the dockerId in the snippet.
If you are using a private repo like harbor or artifactory, you may need the permission to push to it.
docker build . -t dockerId/Name-of-your-image-you-want:latest
Upload the docker image using docker push
docker push dockerId/Name-of-your-image-you-want:latest
change the image tag in your gitlab-ci.yaml to dockerId/Name-of-your-image-you-want:latest
I found a solution.
- sudo cp jdk-17-linux-x64.tar.gz /usr/lib/jvm
- sudo tar zxvf "/usr/lib/jvm/jdk-17-linux-x64.tar.gz" -C /usr/lib/jvm
- sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-1.11.0-openjdk-amd64
- sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/default-java
- sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/java-11-openjdk-amd64
- sudo \cp -r /usr/lib/jvm/jdk-17 /usr/lib/jvm/openjdk-11
- sudo update-alternatives --remove-all java
- sudo update-alternatives --remove-all javac
- sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-17/bin/java 1
What I did here was to copy the JDK-17 content to all folders from /usr/lib/jvm folder. So even though the docker image uses JDK-11, I'm rewriting it using JDK-17 uploaded with the source code, and now the tool is built using JKD-17.
PS: I know this is slower and not professional, but in my case, it's easier and more convinient than trying to get help from those who setup the docker container.

How to install Oracle Java 8 on Ubuntu 18.04?

Previously, there was an easy way to install Java 8 on Ubuntu using webupd8team repository. But currently it does not work and packages are not located.
So after spending a lot of effort on such a common case I decided to add this post.
I used several resources to achieve it.
Uninstall openjdk sudo apt-get purge openjdk*
Go to this blog post and run all steps until 4. If you have issues on step #3 go to step 3 in this post (sorry for numbering :))
sudo mkdir /usr/lib/jvm-oracle
sudo cp ~/Downloads/(name of your tarball) /usr/lib/jvm-oracle
cd /usr/lib/jvm-oracle
sudo tar -xvzf (name of tarball)
cd jdk1.8.0_(corresponding version)
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm-oracle/jdk1.8.0_{version}/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm-oracle/jdk1.8.0_{version}/bin/javac 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm-oracle/jdk1.8.0_{version}/bin/javaws 1
sudo cat > /etc/profile.d/jdk.sh
export JAVA_HOME="/usr/lib/jvm-oracle/jdk1.8.0_221"
export PATH="$PATH:${JAVA_HOME}/bin"
Ctrl+d
(Optional) Make sure java alternatives are updated sudo update-alternatives --config java
Update
You can download Java from here
Download the JDK 8 SDK.
Create a directory at /usr/lib called jvm-oracle. You will need to use the sudo command as this directory is at root level:
sudo mkdir /usr/lib/jvm-oracle
Copy your tarball over:
sudo cp ~/Downloads/(name of your tarball) /usr/lib/jvm-oracle
Move into the /usr/lib/jvm-oracle and extract your tarball:
Move into: cd /usr/lib/jvm-oracle
Extract: sudo tar -xvzf (name of tarball)
List out the directory contents and find your extracted folder:
ls -al
You should see a directory like ‘jdk1.8.0_172’. Move into your dir and the bin folder with cd and list out the contents.
Move to new dir: cd jdk1.8.0_172.
Move to bin: cd bin
List out Contents: ls -al
Run these following commands:
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm-oracle/jdk1.8.0_172/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm-oracle/jdk1.8.0_172/bin/javac 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm-oracle/jdk1.8.0_172/bin/javaws 1
Then add the JAVA_HOME by these command:
echo $'\nJAVA_HOME='`which java` >> ~/.bashrc
Alternatively, you can add it manually by opening your .bashrc file and setting
JAVA_HOME=/usr/bin/java

Install Java 8 in debian Jessie

I've tried to install Java 8 into a ARM embedded linux in several ways but none of them worked:
First:
http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
(from the repositories of webupd8)
W: Failed to fetch
http://ppa.launchpad.net/webupd8team/java/ubuntu/dists/trusty/InRelease
Unable to find expected entry 'main/binary-armel/Packages' in Release
file (Wrong sources.list entry or malformed file)
W: Failed to fetch
http://ppa.launchpad.net/webupd8team/java/ubuntu/dists/jessie/main/binary-armel/Packages
404 Not Found
E: Some index files failed to download. They have been ignored, or old
ones used instead.
It seems that this repo does not have the source for my architecture:
Architectures: amd64 arm64 armhf i386 powerpc ppc64el
And i need armel ( at least this is working for java 7 )
I also tried this way:
http://www.rpiblog.com/2014/03/installing-oracle-jdk-8-on-raspberry-pi.html
Downloading the jdk from Oracle and then following the instructions.
But i cannot execute the file :
root#arietta:~# java -version
-bash: /usr/bin/java: No such file or directory
and neither:
root#arietta:~# /opt/jdk1.8.0_71/bin/java -version
-bash: /usr/bin/java: No such file or directory
While the file exists and has the correct permissions... i'm going crazy..
Any idea or alternative method?
At the end i solved it adding jessie backports to the sources.list:
echo deb http://http.debian.net/debian jessie-backports main >> /etc/apt/sources.list
apt-get update && apt-get install openjdk-8-jdk
update-alternatives --config java
I ran the following commands from the webupd8 team and it worked for me:
http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html
su -
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | tee /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
apt-get update
apt-get install oracle-java8-installer
exit
Same as #user2528085, you just need to add Debian backports to your sourcelist file.
Follow this instruction on Debian official site
https://backports.debian.org/Instructions/
Run these commands in shell:
echo "deb http://ftp.debian.org/debian jessie-backports main" | sudo tee -a /etc/apt/sources.list.d/jessie-backports.list
sudo apt-get update && sudo apt-get install elasticsearch
Nothing difficult

update-alternative too many symbolic links

I am trying to install Juniper Network connect 32 bit on my 64 bit Ubuntu 14.10. following the documentation here
Network connect needs the java 32 bit to be installed along with the 64 java.
those are some commands i used while installing java 64:
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.8.0_31/bin/java" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.8.0_31/bin/javaws" 1
sudo update-alternatives --set javaws /usr/local/java/jre1.8.0_31/bin/javaws
Next I installed java 32 under this directory Usr/local/Java32/jre.8.0_31.
Im trying as written in the tutorial to make Network Connect link to Java32 instead of java64 while jave 64 is the default. so I'm using this command line:
sudo update-alternatives --install /usr/bin/java java /usr/java32/jre1.8.0_31/bin/java 10.
But it is not working! i tried it different times with different changes and ofcourse i got this message:
too many levels of symbolic links
how do i undo all the update-alternatives i made? and what is the right way to link to java 32?
Use this command to start network connect which i am reproducing from the same juniper's page (there is a bug in the command. use -jar & not -cp as given by them). please note the NC.jar sits in your $HOME/.juniper_networks/
And in the command, for give java 6 bin path (in your case, /usr/java32/hre1.8.0_31/bin/java)
<java_path> -jar NC.jar -h <ivehostname> -u <username> -p <password> [-r <realm>]
-f <ivecertificate_in_der_format> [-l <gui_log_level> [-L <ncsvc_log_level>] [-y <proxy> -
z <proxy_port> [-s <proxy_username> -a <proxy_password> [-d <proxy_domain>]]]

Categories