Code in bash script that changes version of java - bash

I have to preparae a script that
-switches the current Java to this one: /usr/bin/java
export PATH=$JAVA_HOME”$PATH” export JAVA_HOME=”/usr/bin/java”
What should I add to it to work?

/usr/bin/java is not a directory, but an executable file.
The JAVA_HOME should point to Java's installation directory, if at all required. Most of the time, you don't actually need to define JAVA_HOME as it's only used by certain applications (such as Maven) to override the version of Java that would otherwise be found on the PATH.
So all you need to do is:
export PATH=/usr/bin:$PATH
and this will make the java command be taken from the /usr/bin directory. This is normally in your path anyway, but this line ensures it's at the start.
You may also need to delete any existing definition of the JAVA_HOME variable, as that would override /usr/bin/java when used by tools such as Maven.

Related

How can I change the JAVA_HOME directory permanently on MAC?

I have a problem with my JAVA_HOME directory. Every time I want to use gradle I need to set my JAVA_HOME directory again and again.
I know how to export it to the right directory but after I close my terminal, I have to do it all over again.
What I do is
which java --> /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/bin/java
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/"
to set the correct path. Everything is then set correctly, but when I close the terminal and get check the path again:
echo $JAVA_HOME -> /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Homeexport
Can you guys tell me how to change the directory permanently? I googled so often and found solutions which didn't help me at all
This isn't so much a Gradle question, as an environment question. You would need to set JAVA_HOME in your shell's configuration. If you can tell me what shell you're using, I can point you in the right direction.
Since you're using ZSH, you can open ~/.zshenv and paste this in:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/"
Another solution would be to use something like Jenv to manage your Java environment, with the Gradle plugin enabled.

Is there a robust way to set MAVEN_HOME in my ~/.zshrc without specifying the version number? (MacOS)

Context: I install maven with brew install maven
In my ~/.zshrc I have:
# Maven (mvn --version)
export MAVEN_HOME="$HOME/usr/local/Cellar/maven/3.6.3"
export PATH=$MAVEN_HOME/bin:$PATH
Is there a way to set my MAVEN_HOME in a way that allows me to not have to increment it when Maven is upgraded to 3.6.4?
It is somewhat tedious to have to go into my config file and update it every time I update Maven! :)
This would also help an onboarding/setup script that could download maven and programmatically set up a ~/.zshrc or ~/.bashrc config
What i have done to solve this problem is to create a symbolic link that points to maven installation version.
ln -s maven_3.6.4_path maven_slink
And set the MAVEN_HOME=maven_slink in configuration file. And every time i need to change maven version all i have to do is drop maven_slink and create a new maven_slink that points to the maven installation. I don't need to go change configuration files every time I install a new version of maven.
Per #chepner: /usr/local/bin/mvn is a symlink to the latest installed version when installed via homebrew.
I changed export MAVEN_HOME="/usr/local/Cellar/maven/3.6.3/mvn" to export MAVEN_HOME="/usr/local/bin/mvn" in my ~/.zshrc config and everything is good to go.
I work on windows and i have created folder dev-env and the path of this folder i have added in PATH variable.
I have installed the version of java jdk8, jdk9, jdk11, jdk13.
I have created bat files and place them in dev-env : jdk8.bat ,jdk9.bat, jdk9.bat, jdk11.bat and jdk13.bat.
Inside a bat files I do the switch of version of java.
#echo off
set JAVA_HOME=path to jdk version
set PATH = %JAVA_HOME%\bin+PATH
java -version
Same thing in maven . I created different bat files maven3.bat, maven36.bat.
#echo off
set MAVAEN_HOME=path to jdk version
set PATH = %MAVEN_HOME%\bin+PATH
mvn -version
So , If i want to switch from one version to another. I open the cmd and the run the bat file that has the version that i want.

HADOOP_HOME is not set correctly

I downloaded the binary tarball of hadoop from here: http://hadoop.apache.org/releases.html (ver 2.8.4). I unpacked the tar.gz file and then changed the etc/hadoop-env.sh from
export JAVA_HOME={$JAVA_HOME}
to my java jdk locaction:
export JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
I also added these two lines:
export HADOOP_HOME=D:/hadoop/hadoop-2.8.4
export PATH=$PATH:$HADOOP_HOME/bin
But when i try to run
$ hadoop version
from cmd i get an error message that says
Error: HADOOP_HOME is not set correctly
What did I do wrong and how should I change the hadoop_home path for it to work?
Other than {$JAVA_HOME} has the dollar sign in the wrong spot (needs to be outside the brackets), Windows doesn't run the shell script to locate your variables
You need to set environment variables in Windows from the Control Panel. And you also need to remove all spaces from the file path of "Program Files"
Its not clear if you're using Cygwin or using Windows Linux subsystem, but it's different from the native CMD
Set the path HADOOP_HOME Environment variable as below:
export HADOOP_HOME=D:\hadoop\hadoop-2.8.4
export PATH=$PATH:$HADOOP_HOME\bin
$ hadoop version
It will work
I come across such error when I try to use hadoop-3.3.1, the latest version. I have searched a lot about "HADOOP_HOME not correctly set" and there are no useful results.
But after I downgrade to hadoop-3.2.2, this error disappears.
I think you can try the non-latest version again.

why this JRE error happening during maven build

Even though I set the JAVA_HOME and PATH to point to the JDK7 directory, Maven takes the /usr/bin/ as JVM path and It searches for the tools.jar file in the JRE and throws the below error.
Fatal error compiling
Embedded error: tools.jar not found: /usr/lib/jvm/java-7-openjdk-amd64/jre/../lib/tools.jar
But if i set the /usr/bin/java to point to the JDK directory manually, it works fine. I am puzzled with this behavior. Can someone explain why it is like that ?
Close terminal and reopen it and check whether $PATH is Proper.
echo $JAVA_HOME -or-
echo $PATH
Assuming that you have a linux machine.
Take a look on /usr/bin/java, this is a symbolic link. Look where is a target of this symbolic link (in my case the target is at this location /etc/alternatives/java)
/etc/alternatives/java is symbolic link too. Create new symbolic link to "proper" JVM (e.g. Sun's JVM)
Replace /etc/alternatives/java with newly created symbolic link by /usr/sbin/alternatives --install /usr/bin/java java /usr/java/latest/bin/java 2

How to use different java versions for different programs?

This may be common in web but may be I am not getting what to search exactly. Actually I am having different versions of java on my ubuntu server having java 1.5 as default. One of my program need java 1.6 and even if I perform following in the shell script executing that program
export JAVA_HOME=/path/to/jdk6
I dont get any success.What I learnt is I could see current java version using
java -version
This only get change when I changes the alternative using:
sudo update-alternatives --config java
Please tell me how can I change it in the shell script which in turn calls the java program requiring java6 having java 5 as default only.
Thanks!!!
You need to update the PATH as well:
JAVA_HOME=/path/to/jdk6
export JAVA_HOME
PATH=${JAVA_HOME}/bin:${PATH}
export PATH

Resources