Install Java7 on Ubuntu 16.04 through bash script - java

I have below function java_install written in a bash script to install java on Linux box, to which I pass jdk-1.7.0_80-linux-x64.tgz as JAVA_PACKAGE.
Now what is happening is java gets installed and works fine only within the script. Once I come out of this script, none of the java functionalities work, not even java -version. Could someone please help me on what I might be missing here? Basically, I just want java to be installed permanently on this box once this script is executed.
java_install() {
local JAVA_PACKAGE=$1
local TMPDIR=/tmp/quickstart
local TARGET=/usr/share
if [ -n "$JAVA_PACKAGE" ] && [ -f "$JAVA_PACKAGE" ]; then
rm -rf $TMPDIR
mkdir -p $TMPDIR
cp $JAVA_PACKAGE $TMPDIR
( cd $TMPDIR && tar fxz $JAVA_PACKAGE && rm $JAVA_PACKAGE )
local JAVA_BASENAME=$(ls -1 $TMPDIR)
mkdir -p $TARGET
if [ -d "$TARGET/$JAVA_BASENAME" ]; then
echo "# Java already installed at $TARGET/$JAVA_BASENAME"
log_info "Java already installed at $TARGET/$JAVA_BASENAME"
else
echo "# Java now installed at $TARGET/$JAVA_BASENAME"
log_info "Java now installed at $TARGET/$JAVA_BASENAME"
mv $TMPDIR/$JAVA_BASENAME $TARGET
fi
rm -rf $TMPDIR
# now create a script to export these settings
export JAVA_HOME=$TARGET/$JAVA_BASENAME
export PATH=$JAVA_HOME/bin:$PATH
else
echo "# cannot find java package to install"
log_error "cannot find java package to install"
fi
}

Use update alternatives within your script to make your java installation available:
sudo update-alternatives --install "/usr/bin/java" "java" "path to you java executable" 1
More information on this topic can be found here: How to use the command update-alternatives --config java.
Alternatively you can write the export commands for JAVA HOME and PATH to your .bashrc from within your script (if using bash). This way the modified variables are available in the bash shell.

Related

Install Java 16 on Raspberry Pi 4

I have tried multiple releases from here using :
sudo -i
cd /usr/lib/jvm
wget [release link here]
tar xzf [file name here]
export PATH=$PWD/[dir here]/bin:$PATH
java -version
But after java -version I always get:
/usr/lib/jvm/[dir here]/bin/java: cannot execute binary file: Exec format error
Which means I have chosen the wrong release/architecture. Is there a release that works with Raspberry Pi's or is there another way to install Java 16?
Answering my own question.
cd [minecraft directory here]
wget https://github.com/AdoptOpenJDK/openjdk16-binaries/releases/download/jdk16u-2021-05-08-12-45/OpenJDK16U-jdk_arm_linux_hotspot_2021-05-08-12-45.tar.gz
tar xzf OpenJDK16U-jdk_arm_linux_hotspot_2021-05-08-12-45.tar.gz
export PATH=$PWD/jdk-16.0.1+4/bin:$PATH
java -version
You might have to do
export PATH=$PWD/jdk-16.0.1+4/bin:$PATH
after a reboot

getting the error "TERM environment variable not set" when running a bash script from Java process builder [duplicate]

I have a file.sh with this, when run show : TERM environment variable not set.
smbmount //172.16.44.9/APPS/Interfas/HERRAM/sc5 /mnt/siscont5 -o
iocharset=utf8,username=backup,password=backup2011,r
if [ -f /mnt/siscont5/HER.TXT ]; then
echo "No puedo actualizar ahora"
umount /mnt/siscont5
else
if [ ! -f /home/emni/siscont5/S5.TXT ]; then
echo "Puedo actualizar... "
touch /home/emni/siscont5/HER.TXT
touch /mnt/siscont5/SC5.TXT
mv -f /home/emni/siscont5/CCORPOSD.DBF /mnt/siscont5
mv -f /home/emni/siscont5/CCTRASD.DBF /mnt/siscont5
rm /mnt/siscont5/SC5.TXT
rm /home/emni/siscont5/HER.TXT
echo "La actualizacion ha sido realizada..."
else
echo "No puedo actualizar ahora: Interfaz exportando..."
fi
fi
umount /mnt/siscont5
echo "/mnt/siscont5 desmontada..."
You can see if it's really not set. Run the command set | grep TERM.
If not, you can set it like that:
export TERM=xterm
Using a terminal command i.e. "clear", in a script called from cron (no terminal) will trigger this error message. In your particular script, the smbmount command expects a terminal in which case the work-arounds above are appropriate.
You've answered the question with this statement:
Cron calls this .sh every 2 minutes
Cron does not run in a terminal, so why would you expect one to be set?
The most common reason for getting this error message is because the script attempts to source the user's .profile which does not check that it's running in a terminal before doing something tty related. Workarounds include using a shebang line like:
#!/bin/bash -p
Which causes the sourcing of system-level profile scripts which (one hopes) does not attempt to do anything too silly and will have guards around code that depends on being run from a terminal.
If this is the entirety of the script, then the TERM error is coming from something other than the plain content of the script.
You can replace :
export TERM=xterm
with :
export TERM=linux
It works even in kernel with virgin system.
SOLVED: On Debian 10 by adding "EXPORT TERM=xterm" on the Script executed by CRONTAB (root) but executed as www-data.
$ crontab -e
*/15 * * * * /bin/su - www-data -s /bin/bash -c '/usr/local/bin/todos.sh'
FILE=/usr/local/bin/todos.sh
#!/bin/bash -p
export TERM=xterm && cd /var/www/dokuwiki/data/pages && clear && grep -r -h '|(TO-DO)' > /var/www/todos.txt && chmod 664 /var/www/todos.txt && chown www-data:www-data /var/www/todos.txt
If you are using the Docker PowerShell image set the environment variable for the terminal like this with the -e flag
docker run -i -e "TERM=xterm" mcr.microsoft.com/powershell

cumulocity's rasberrypi's "c8y-agent.sh" file not working

I am trying to connect my cumulocity-rpi-agent to cumulocity so that I can have two options either accept or cancel. But this options will be available only when i execute sh c8y-agent-debug.sh this file using sh command. I followed below steps
1) wget http://resources.cumulocity.com/examples/cumulocity-rpi-agent-latest.deb
2) sudo dpkg -i cumulocity-rpi-agent-latest.deb
And:
login as: pi
pi#raspberrypi.mshome.net's password:
...
pi#raspberrypi:~ $ cd /usr/share/
pi#raspberrypi:/usr/share $ cd cu
cumulocity-rpi-agent/ cups/
pi#raspberrypi:/usr/share $ cd cumulocity-rpi-agent/
pi#raspberrypi:/usr/share/cumulocity-rpi-agent $ sh c8y-agent.sh
^C
pi#raspberrypi:/usr/share/cumulocity-rpi-agent $ sudo nano
pi#raspberrypi:/usr/share/cumulocity-rpi-agent
And:
#!/bin/sh
(
while true
do
echo "Running the Cumulocity Linux Agent..."
java -cp 'cfg/*:lib/*' -Dlogback.configurationFile=cfg/logback.xml c8y.lx.age$
sleep 1
done
) 2>&1 | logger
this is c8y-agent-debug.sh script I execute this file using sh c8y-agent-debug.sh or bash c8y-agent-debug.sh but executing the file not event echo statement.
Do i need to set anything related to java. Java is already installed. Is there any additional setting required for that java -cp command. Java is installed on /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/ on this path. This is on linux. javac and java commands are working fine

which: no java for my user on linux

I try to install java8 on my Linux server centOS. In /etc/profile
export JAVA_HOME=/root/jdk1.8.0_71/
export PATH=$JAVA_HOME/bin/:$PATH
[root#localhost ~]# echo $JAVA_HOME
/root/jdk1.8.0_71/
[root#localhost ~]# which java
/root/jdk1.8.0_71/bin/java
[root#localhost ~]#
And then I install Cassandra, but it cannot find the java8. And I execute some commands and get
[root#localhost ~]# su cassandra -c "echo $JAVA_HOME"
/root/jdk1.8.0_71/
[root#localhost ~]# su cassandra -c "whereis java"
java: /usr/bin/java /etc/java /usr/lib/java /usr/local/bin/java /usr/share/java /usr/share/man/man1/java.1.gz
[root#localhost ~]# su cassandra -c "which java"
which: no java in (/root/jdk1.8.0_71//bin/:/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
I want to know since the PATH is right, why I failed to get java using which java?
And then I create soft link and change the mode of /root/jdk1.8.0_71 to 777
cd /usr/bin
java -> /root/jdk1.8.0_71/bin/java
But it is still not work. Did I miss somethings or make anything wrong?
If the user Cassandra does not have permission on the /root folder then it wont be able to find it. I don't know why would you copy the JDK in the /root folder. My suggestion is to copy the jdk1.8.0_71 in the /opt folder and set the correct permission (owner or group), something like:
cp -r /root/jdk1.8.0_71 /opt
chown -R cassandra.cassandra /opt
Additional, setting paths in the PATH variable should not include the trailing to avoid "/root/jdk1.8.0_71//bin/"

Is there any Java version manager?

I used to have multiple JDK installed on my Linux machine, and I like to switch from one version to another from the command-line ( I used to change my JAVA_HOME manually):
This is my current approach :
I source a ~/.paths in my .bashrc.
the .paths contain all the JDK installed on my machine.
JDK7="~/local/jdk1.7.0_15"
JDK8="~/local/jdk1.8.0"
// I use Jdk 7 by default
JDK_HOME=$JDK7;
// including $JDK_HOME/bin to the $PATH
When I want t switch to JDK8, I modify the JDK_HOME variable to point on JDK8 in the file, and I re-source my .paths file.
I know that IDE can manage multiple JDK easily, but I want an rvm like solution.
Is there any better trick ?
Better more, Is there any equivalent for rvm in Java ?
There is jdk_switcher although it is quite static to some ubuntu paths - it should be easy to modify it to run from other paths.
There is a plan to make RVM 2 support switching more then just Ruby versions, you can read more about it here.
anyone with the link can comment.
I don't think there is such solution.
See this question to a solution with symlinks.
Now , I'm using jdk-manager, a little bash script to manage multiple JDKS installations.
You can have as many Java versions installed as you want to. Just install to a folder of your choosing and use some convention.
If you want to make one-time use of a particular version run it with a full path (for example):
>C:\java\jdk-6u35\bin\java.exe
or
>/java/jdk-6u35/bin/java.exe
If you want to change to just use it, change your path to put the version you want at the front. The path might be similar to that shown above.
Be sure to change JAVA_HOME as well and any other environment settings that include a reference to the java location.
Note that some tools have internal configuration as to which java version to use. Eclipse is a good example. You have to set up a list of your JVMs and then select one for each project or for all projects.
I wrote my own script to manage java versions. I use some sites that require Oracle Java and usually the latest version, so I can't use apt/aptitude and therefore can't use update-alternatives or jdk-manager (which uses update-alternatives).
Here's my script. I don't develop professionally, so it's probably a mess, but it serves my purposes. It only considers java versions stored in /usr/lib/jvm.
#!/bin/bash
# the proper way....
# + update: update-alternatives only works for versions installed by apt or aptitude
# ... same for jdk-manager (uses update-alternatives to do the heavy lifting)
#echo "The proper way is:"
#echo "$ update-alternatives --config java"
#exit
# the rest is (no longer) depreciated....
echo "The current default java is $(readlink --canonicalize `which java`)"
PS3='Select Java to install: '
options=( $(find /usr/lib/jvm -iname java) )
noptions=${#options[#]}
(( loption=noptions-1 ))
options[${#options[#]}]="Quit"
select opt in "${options[#]}"
do
for i in $(seq 0 $loption); do
[ "$opt" == "${options[$i]}" ] && \
javapath=${options[$i]}
done
if [ "$javapath" ]; then
break
fi
if [ "$opt" == "Quit" ]; then
echo "Nothing installed.";
exit
else
echo "Invalid option. Try another one.";
continue
fi
done
# remove the old link (might be superfluous)
#rm -vf -- "$link"
# set new link (symbolic, force, verbose)
sudo ln -sTfv "$javapath" "/usr/bin/java"
default_java_dir=$(echo "$javapath" | grep --only-matching --regexp="\/usr\/lib\/jvm\/[^\/]*")
sudo ln -sTfv "$default_java_dir" "/usr/lib/jvm/default-java"
java_bin_dir=$(echo "$javapath" | sed 's/[^\/]*$//')
echo $java_bin_dir
[ -f "${java_bin_dir}javac" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javac"
[ -f "${java_bin_dir}javadoc" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javadoc"
[ -f "${java_bin_dir}javafxpackager" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javafxpackager"
[ -f "${java_bin_dir}javah" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javah"
[ -f "${java_bin_dir}javap" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javap"
[ -f "${java_bin_dir}java-rmi.cgi" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}java-rmi.cgi"
[ -f "${java_bin_dir}java_vm" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}java_vm"
[ -f "${java_bin_dir}javaws" ] && sudo ln -sfv -t "/usr/bin" "${java_bin_dir}javaws"
find_dir=$(dirname "$java_bin_dir")
pluginpath=$(find "$find_dir" -name libnpjp2.so)
#exit
echo -n "Install $pluginpath as the Java plugin? [y/N]: "
read
response=$(echo $REPLY | sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
if [ "${response:0:1}" == "Y" ]; then
# directories for plugins
plugin_dirs="/usr/lib/firefox/plugins
/usr/lib/firefox-addons/plugins
/usr/lib/mozilla/plugins
/opt/google/chrome
/home/james/.mozilla/plugins"
# first, clean out anything we might have improperly installed already
echo "Using 'sudo' to remove any installed java plugins..."
for pdir in $plugin_dirs; do
sudo rm --verbose --force "$pdir/libjavaplugin_oji.so" "$pdir/libnpjp2.so" "$pdir/IcedTeaPlugin.so"
done
# okay, trying brute force and awkwardness....
echo "Using 'sudo' to install \"$pluginpath\" in several places..."
for pdir in $plugin_dirs; do
sudo ln --symbolic --verbose --force "$pluginpath" "$pdir"
done
fi
exit
If you use Debian or a derived GNU/Linux distribution, you can use update alternatives to set what is currently being run when you type java.
Try typing
update-alternatives --display java
That will show you what alternatives you have available.
This command is non-destructive, that is, it doesn't change anything.
man update-alternatives
Will give you the manual for the tool.
The command you will likely want though is:
update-alternatives --config java
Which will give you a simple, interactive way of setting the java program. You can also use --set if you want to script it.
Of course, you should not trust me without running man first, because people sometimes go on the Internet and tell lies. ;)
EDIT: I forgot, this link introduces update-alternatives in a good (if Vi specific) way.
For Linux, you can use update-alternatives to not only set paths to java, javac, and other binaries but also your JAVA_HOME. Since all it does is manage links, you can install a link to your jdk directories and then set JAVA_HOME to point to that link. For example, "update-alternatives --install /usr/lib/jdk jdk /path/to/jdk8 1" will install a link to your jdk directory. You then add "export JAVA_HOME to /usr/lib/jdk" in .bashrc, .profile, or whatever file you use to set environment variables and any alternative you install under the name jdk will be pointed to by JAVA_HOME when you switch with update-alternatives --config jdk. If you are using alternatives already for the java executables, you can use --slave to make jdk whenever java does.

Categories

Resources