Difference between using JAVA_HOME=... and export JAVA_HOME=$(...) - bash

Up until now I've been setting environmental variables using something like:
JAVA_HOME=...
From this https://xenovation.com/blog/development/java/how-to-set-java-home, I can set it using:
export JAVA_HOME=$(/usr/libexec/java_home)
Could someone explain:
Why echo $JAVA_HOME shows /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
I understand that export JAVA_HOME=... sets an environment variable. What does adding $(...) do?
Would export JAVA_HOME=(/usr/libexec/java_home) be the same thing as export JAVA_HOME=/usr/libexec/java_home?
Thank you!

1- /usr/libexec/java_home is an executable that can give you proper value for JAVA_HOME, it also can be used by arguments. for example /usr/libexec/java_home -v '1.7*' will give you sutiable value for JAVA_HOME in order to use Java 7. $(/usr/libexec/java_home) will give you path to JAVA_HOME which in your case will be /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home.
2- $(...) is for command substitution. for example:
a=$(echo foo) # results a="foo"
3- they're not the same in concept! export JAVA_HOME=(/usr/libexec/java_home) will assign an array to JAVA_HOME but export JAVA_HOME=/usr/libexec/java_home ill assign an string path to it. if you echo $JAVA_HOME it will show the first element of array which in your case because you have only one element, it will result into the same thing. It means export JAVA_HOME=(/usr/libexec/java_home) and export JAVA_HOME=/usr/libexec/java_home will be the same.
NOTE: /usr/libexec/java_home is only a binary file which points to real JAVA_HOME. you must execute it with export JAVA_HOME=$(/usr/libexec/java_home) to get true result!

Related

Exporting environment variables in ~/.bash_profile produces wrong values

On my MacBook with MacOS Catalina I try to export multiple environment variables by setting them in the ~/.bash_profile file. I proceed as follows:
touch ~/.bash_profile
open ~/.bash_profile
Then the file ~/.bash_profile opens in my text editor and I add multiple environment variables:
export VAR1="some_value"
export VAR2="some_value"
export VAR3="some_value"
export VAR4="some_value"
export VAR5="some_value"
Then I save the file and apply the changes as follows:
source ~/.bash_profile
Now I should be able to access the values of the newly defined variables. If I type echo ${VAR1} or echo ${VAR2} I get the correct value some_value, but if I type echo ${VAR3} I get export$lue, i.e. the first seven characters are replaced by export$.
Any advice?

Setting PYTHONPATH on ubuntu fails with 'not a valid identifier'

I am trying to set the Pythonpath on a Ubuntu server without luck.
export PYTHONPATH=$PYTHONPATH:/home/ubuntu/canonicaliser_api
This executes without any errors, however when I want to double check the variable.
export $PYTHONPATH
I get this:
-bash: export: `:/home/ubuntu/canonicaliser_api': not a valid identifier
I can't find anything on google. What is causing this?
The error shown is because you are exporting the value of the shell variable PYTHONPATH which is the path that you specified. Do this instead:
export PYTHONPATH
which tells the shell to export the variable named PYTHONPATH, not its value.
If you simply want to see the value of the variable use
echo $PYTHONPATH
instead of export.

Modifying $PATH variable by a bash alias

I created this alias in my .bashrc file:
alias changepath="export PATH=$JAVA_HOME/bin:$PATH"
What I want to do is change $JAVA_HOME and then change the $PATH to include the new $JAVA_HOME/bin.
When I change $JAVA_HOME and type this command instead of using the alias, it works as expected.
However, when I change $JAVA_HOME and then use the alias, the changepath command prepends the old $JAVA_HOME/bin instead of the new $JAVA_HOME/bin to $PATH.
How can I fix this?
You can also defer evaluation of variables by single-quoting your alias definition, i.e.
alias changepath='export PATH=$JAVA_HOME/bin:$PATH'
Note that the alias or function solutions do not solve the problem of clearing any previous JAVA_HOME you have set.
So you can wind up with a crazy PATH
PATH="/usr/bin:/sys"
PATH=/usr/bin/java:/usr/bin:/sys
PATH=/usr/bin/java/bin:/usr/bin/java:/usr/bin:/sys
Try to use a function instead:
changepath () {
export PATH="$JAVA_HOME/bin:$PATH";
}
In your case "$JAVA_HOME and $PATH are interpreted when you set the alias. So they are fixed to the moment when you execute the .bashrc.
With a function, variables will be interpreted at launching.
Note: I copy/paste you export PATH=..., but be careful you will add $PATH's content at every launch.
This is how I approached the problem of changing between Java 7 and 8 SDKs. I am sure someone could improve these bash functions but I guess the important part is using sed to edit the path.
This will find and replace the JAVA_HOME value rather than keep adding to the PATH variable.
function java7 () {
export PATH=$(echo $PATH | sed s~$JAVA_HOME~${JAVA_7_HOME}"/bin"~)
export JAVA_HOME="${JAVA_7_HOME}";
}
function java8 () {
export PATH=$(echo $PATH | sed s~$JAVA_HOME~${JAVA_8_HOME}"/bin"~)
export JAVA_HOME="${JAVA_8_HOME}";
}
I need to switch between 8 and 11 so what I did was:
Add NO_JAVA_PATH in .profile
export NO_JAVA_PATH=$PATH
export JAVA_HOME="<path>"
export PATH=$JAVA_HOME/bin:$PATH
Added aliases in .bashrc
alias j8='export JAVA_HOME="<path>" ; export PATH=$JAVA_HOME/bin:$NO_JAVA_PATH'
alias j11='export JAVA_HOME="<path>" ; export PATH=$JAVA_HOME/bin:$NO_JAVA_PATH'

what is the difference between these two export statements on JAVA_HOME?

I set up my JAVA_HOME using export JAVA_HOME=/usr/libexec/java_home as recommend by Apple. but it failed. This link referred to use export JAVA_HOME=$(/usr/libexec/java_home) and that worked.
so what is the difference between these two?
export JAVA_HOME=/usr/libexec/java_home
export JAVA_HOME=$(/usr/libexec/java_home)
java_home is a tool that is intended to help determine the correct value for the JAVA_HOME environment variable. From the man-page (man java_home):
The java_home command returns a path suitable for setting the JAVA_HOME
environment variable. It determines this path from the user's enabled and
preferred JVMs in the Java Preferences application. Additional constraints
may be provided to filter the list of JVMs available. By default, if no
constraints match the available list of JVMs, the default order is used.
The path is printed to standard output.
Now, in bash, surrounding a command with $( ... ) inserts the standard output printed by the enclosed command. So, your export statement:
export JAVA_HOME=$(/usr/libexec/java_home)
... means "set JAVA_HOME to the value printed by running /usr/libexec/java_home."
Without the surrounding $(...), the export just sets the JAVA_HOME variable to the literal string "/usr/libexec/java_home", which is certainly not what you want.

Working With Hadoop: localhost: Error: JAVA_HOME is not set

I'm working with Ubuntu 12.04 LTS.
I'm going through the hadoop quickstart manual to make a pseudo-distributed operation. It seems simple and straightforward (easy!).
However, when I try to run start-all.sh I get:
localhost: Error: JAVA_HOME is not set.
I've read all the other advice on stackoverflow for this issue and have done the following to ensure JAVA_HOME is set:
In /etc/hadoop/conf/hadoop-env.sh I have set
JAVA_HOME=/usr/lib/jvm/java-6-oracle
export JAVA_HOME
In /etc/bash.bashrc I have set
JAVA_HOME=/usr/lib/jvm/java-6-oracle
export JAVA_HOME
PATH=$PATH:$JAVA_HOME/bin
export PATH
which java returns:
/usr/bin/java
java –version works
echo $JAVA_HOME returns:
/usr/lib/jvm/java-6-oracle
I've even tried becoming root and explicitly writing the in the terminal:
$ JAVA_HOME=/usr/lib/jvm/java-6-oracle
$ export JAVA_HOME
$ start-all.sh
If you could show me how to resolve this error it would be greatly appreciated.
I'm thinking that my JAVA_HOME is being overridden somehow. If that is the case, could you explain to me how to make my exports global?
I am using hadoop 1.1, and faced the same problem.
I got it solved through changing JAVA_HOME variable in /etc/hadoop/hadoop-env.sh as:
export JAVA_HOME=/usr/lib/jvm/<jdk folder>
The way to solve this problem is to export the JAVA_HOME variable inside the conf/hadoop-env.sh file.
It doesn't matter if you already exported that variable in ~/.bashrc, it'll still show the error.
So edit conf/hadoop-env.sh and uncomment the line "export JAVA_HOME" and add a proper filesystem path to it, i.e. the path to your Java JDK.
# The Java implementation to use. Required.
export JAVA_HOME="/path/to/java/JDK/"
Ran into the same issue on ubuntu LTS 16.04. Running bash -vx ./bin/hadoop showed it tested whether java was a directory. So I changed JAVA_HOME to a folder and it worked.
++ [[ ! -d /usr/bin/java ]]
++ hadoop_error 'ERROR: JAVA_HOME /usr/bin/java does not exist.'
++ echo 'ERROR: JAVA_HOME /usr/bin/java does not exist.'
ERROR: JAVA_HOME /usr/bin/java does not exist.
So I changed JAVA_HOME in ./etc/hadoop/hadoop-env.sh to
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre/
and hadoop starts fine.
The way to debug this is to put an "echo $JAVA_HOME" in start-all.sh. Are you running your hadoop environment under a different username, or as yourself? If the former, it's very likely that the JAVA_HOME environment variable is not set for that user.
The other potential problem is that you have specified JAVA_HOME incorrectly, and the value that you have provided doesn't point to a JDK/JRE. Note that "which java" and "java -version" will both work, even if JAVA_HOME is set incorrectly.
extract from etc/hadoop/hadoop-env.sh
The only required environment variable is JAVA_HOME. All others are
optional. When running a distributed configuration it is best to
set JAVA_HOME in this file, so that it is correctly defined on
remote nodes.
This means its better and advised to set JAVA_HOME here.. even though the existing definition reads the JAVA_HOME variable. Perhaps its not getting the value of JAVA_HOME from previously set value... standard apache manual does not tell this :( :(
This error is coming from Line 180
if [[ -z $JAVA_HOME ]]; then
echo "Error: JAVA_HOME is not set and could not be found." 1>&2
exit 1
fi
in libexec/hadoop-config.sh.
Try echo $JAVA_HOME in that script. If it doesn't recognize,
Find your JAVA_HOME using this:
$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")
and replace the line
export JAVA_HOME=${JAVA_HOME}
in /etc/hadoop/hadoop-env.sh with JAVA_HOME you got from above command.
I also had faced the similar problem in hadoop 1.1
I had not noticed that the JAVA_HOME was commented in: hadoop/conf/hadoop-env.sh
It was
/#JAVA_HOME=/usr/lib/jvm/java-6-oracle
Had to change it to
JAVA_HOME=/usr/lib/jvm/java-6-oracle
regardless of debian or any linux flavor, just know that ~/.bash_profile belongs to specific user and is not system wide.
in pseudo-distributed environment hadoop works on localhost so the $JAVA_HOME in .bash_profile is no use anymore.
just export the JAVA_HOME in ~/.bashrc and use it system wide.
Check if your alternatives is pointing to the right one, you might actually be pointing to a different version and trying to alter the hadoop-env.sh on another installed version.
-alternatives --install /etc/hadoop/conf [generic_name] [your correct path] priority {for further check man page of alternatives}
to set alternatives manually,
alternatives --set [generic name] [your current path].
Change the JAVA_HOME variable in conf/hadoop-env.sh
export JAVA_HOME=/etc/local/java/<jdk folder>
echo "export JAVA_HOME=/usr/lib/java" >> $HADOOP_HOME/etc/hadoop/hadoop-env.sh
Notice: Do not use export JAVA_HOME=${JAVA_HOME} !
I put it on the first line of file ~/.bashrc, then it works well!
export JAVA_HOME=/usr/lib/jvm/default-java

Resources